2010-09-04 107 views

回答

7

您可以创建一个包含比您的图像大40像素的视图。 以40像素偏移量添加图像。 将包含的视图添加为leftBarButtonItem。

代码如下:

// Create a containing view to position the button 
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease]; 

// Create a custom button with the image 
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[barUIButton setImage:barButtonImage forState:UIControlStateNormal]; 
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height); 
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 

[containingView addSubview:barUIButton]; 

// Create a container bar button 
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease]; 

// Add the container bar button 
navigationItem.leftBarButtonItem = containingBarButton; 
1

您可以为显示在导航栏上的图片添加空白区域。我遇到了同样的问题,而且这是我找到解决它的唯一解决方案。有点棘手,但它的工作原理...