1.自定義返回按鈕的問題
通過設置當前頁面的leftBarButtonItem 來替換系統自帶的back 按鈕,下面是系統UINavigationBar的頭文件部分關于leftBarButtonItem 的闡述
@interface UINavigationBar : UIView <NSCoding, UIBarPositioning>
/* By default, the leftItemsSupplementBackButton property is NO. In this case,
the back button is not drawn and the left item or items replace it. If you
would like the left items to appear in addition to the back button (as opposed to instead of it)
set leftItemsSupplementBackButton to YES.
*/
@property(nonatomic) BOOL leftItemsSupplementBackButton NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED;
// Some navigation items want to display a custom left or right item when they're on top of the stack.
// A custom left item replaces the regular back button unless you set leftItemsSupplementBackButton to YES
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@end
意思:一個自定義的left item 將會替換系統的返回按鈕,除非你設置leftItemsSupplementBackButton 屬性為Yes ,back 按鈕才不會被替換
下面是實現代碼:
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"自定義" style:UIBarButtonItemStylePlain target:self action:@selector(clickEvent:)]];
或者設置left items 組合按鈕,比如在webView 的控制器中使用是需要兩個按鈕
UIBarButtonItem * test1 = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickEvent:)];
UIBarButtonItem * test2 = [[UIBarButtonItem alloc] initWithTitle:@"關閉" style:UIBarButtonItemStylePlain target:self action:@selector(clickEvent:)];
[self.navigationItem setLeftBarButtonItems:@[test1,test2]];
如果想讓自己設置的left item 失效,采用系統的back ,采用下面的代碼:
[self.navigationItem setLeftBarButtonItem:nil];
效果如下
自定義返回按鈕.gif
自定義left 組合.gif