您可以將UIImage
添加到UIButton
。然後,將其用作自定義後退按鈕。這裏有一個簡單的例子:
// Custom image
UIImage *backButtonImage = [UIImage imageNamed:@"button-background-image.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 100, 30)];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
// Custom title
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(barPayButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[backButton setShowsTouchWhenHighlighted:NO];
UIBarButtonItem *buttonOnBar =[[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonOnBar;
注:你將失去其在iOS7引入了雪佛龍(系統提供的後退箭頭)。它作爲標題和雪佛龍一起呈現您以前的視圖控制器。
更新:
您還可以使用UIEdgeInsets
智能調整圖像。
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0, 0, 15, 10);
UIImage *backButtonImage = [[UIImage imageNamed:@"button-background-image.png"] resizableImageWithCapInsets:edgeInsets];
我需要添加「返回」標題的圖像來顯示它或有沒有一種方法來設置它與按鈕/圖像組合? – noobsmcgoobs 2014-12-04 05:04:50
@noobsmcgoobs更新了組合。它有點棘手 - 你必須非常謹慎的背景圖像座標。我對它進行了硬編碼(100,30)。一旦你做對了 - 做self.view.frame.size.width(或高度),以適應其中的標題。 – raurora 2014-12-04 05:08:51
是的,它伸出圖像添加文本。我只是要更改按鈕框架並將文本添加到圖像。謝謝。 – noobsmcgoobs 2014-12-04 05:13:30