ios,nav ,rightBarButtonItem 設置多個按鈕的方法

1 ?第一種

self.navigationController.navigationBar.topItem.leftBarButtonItem = nil;

UIToolbar*tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 110, 39)];

//解決出現的那條線

tools.clipsToBounds = YES;

//解決tools背景顏色的問題

[tools setBackgroundImage:[UIImage new]forToolbarPosition:UIBarPositionAny? ? ? ? ? ? ? ? ? ? ? barMetrics:UIBarMetricsDefault];

[tools setShadowImage:[UIImage new]

forToolbarPosition:UIToolbarPositionAny];

//添加兩個button

NSMutableArray*buttons=[[NSMutableArray alloc]initWithCapacity:2];

button3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:myInfo.messageTopImageNameStr] style: UIBarButtonItemStyleDone target:self action:@selector(addressBook)];

UIBarButtonItem*button2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more"] style: UIBarButtonItemStyleDone target:self action:@selector(selectMenu)];

button3.tintColor=[UIColor whiteColor];

button2.tintColor=[UIColor whiteColor];

[buttons addObject:button3];

[buttons addObject:button2];

[tools setItems:buttons animated:NO];

UIBarButtonItem*btn=[[UIBarButtonItem? ? alloc]initWithCustomView:tools];

self.navigationController.navigationBar.topItem.rightBarButtonItem = btn;


2 第二種


//兩個按鈕的父類view

UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

//歷史瀏覽按鈕

UIButton *historyBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

[rightButtonView addSubview:historyBtn];

[historyBtn setImage:[UIImage imageNamed:@"button_history"] forState:UIControlStateNormal];

[historyBtn addTarget:self action:@selector(historyBtnEvent) forControlEvents:UIControlEventTouchUpInside];

#pragma mark >>>>>主頁搜索按鈕

//主頁搜索按鈕

UIButton *mainAndSearchBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 0, 50, 50)];

[rightButtonView addSubview:mainAndSearchBtn];

[mainAndSearchBtn setImage:[UIImage imageNamed:@"button_filter-"] forState:UIControlStateNormal];

[mainAndSearchBtn addTarget:self action:@selector(mainAndSearchBtnEvent) forControlEvents:UIControlEventTouchUpInside];

//把右側的兩個按鈕添加到rightBarButtonItem

UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];

self.navigationItem.rightBarButtonItem = rightCunstomButtonView;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容