我想改變我的UINavigationBar的背面按鈕使用[UINavigationBar的外觀]改變後退按鈕圖像
我可以使用此代碼做到這一點:
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationItem.hidesBackButton = YES;
// Cleanup
[customBarItem release];
,但我已經把那每個viewDidLoad方法中的代碼。我想爲整個項目做一次。
我的嘗試是這樣的:
UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5
resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)];
UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31
// forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
// [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
但它延伸出來的圖像和文字吸引了它,這是我不想要的。我只是想在每個視圖中顯示相同大小的圖像。
謝謝!
工作,但當我在視圖之間來回移動時按鈕閃爍。這並不是什麼大不了的事情,但如果它可以轉換更清潔的 – itgiawa 2012-04-25 00:44:02
以及後面的操作方法在哪裏,那將會很不錯。 – 2015-09-11 09:32:32
不錯的hack!,後退方法swift'self.navigationController?.popViewController(animated:true)' – 2017-03-17 16:00:32