今天mark一下navgationbar的知識袜爪,有時候我們項目需要做像網易云音樂那樣的navgationbar,有多個item同在右邊或者左邊玩耍长酗。
見效果圖,這個是我們要實現的蜘拉。
為了幫助大家學習,我先說下曲線救國的兩種方法有鹿。也是我爬過的坑旭旭。共勉。
- 我嘗試了三種方法印颤,一種是用toolbar放在navgationBar.rightBarButtonItem上
- (void)initNav{
/* CREATE BOOKMARKS BUTTON */
UIButton *bookmarksButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kBookmarksImage.size.width, kBookmarksImage.size.height)];
bookmarksButton.backgroundColor = [UIColor greenColor];
[bookmarksButton setImage:kBookmarksImage forState:UIControlStateNormal];
[bookmarksButton addTarget:self action:@selector(toolbarButtonTapped:) forControlEvents:UIControlEventTouchDown];
bookmarksButton.tag = 1;
UIBarButtonItem *bookmarksButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bookmarksButton];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
spacer.width = SPACE_BETWEEN_BUTTONS;
UIButton *bookmarksButton2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kBookmarksImage.size.width, kBookmarksImage.size.height)];
bookmarksButton2.backgroundColor = [UIColor orangeColor];
[bookmarksButton2 setImage:kBookmarksImage forState:UIControlStateNormal];
[bookmarksButton2 addTarget:self action:@selector(toolbarButtonTapped:) forControlEvents:UIControlEventTouchDown];
bookmarksButton2.tag = 2;
UIBarButtonItem *bookmarksButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:bookmarksButton2];
NSMutableArray<UIBarButtonItem*>*buttons = [NSMutableArray array];
[buttons addObjectsFromArray:@[bookmarksButtonItem,spacer,bookmarksButtonItem2]];
/* ADD ALL THESE BUTTONS TO CUSTOM TOOLBAR AND TOOLBAR TO NAVIGATION BAR */
UIToolbar *customToolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 50, 44.01f)]; // 44.01 shifts it up 1px for some reason
customToolbar.backgroundColor = [UIColor redColor];
customToolbar.clearsContextBeforeDrawing = NO;
customToolbar.clipsToBounds = NO;
customToolbar.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; // closest I could get by eye to black, translucent style.
customToolbar.barStyle = -1; // clear background
[customToolbar setItems: buttons animated:NO];
UIBarButtonItem *customUIBarButtonitem = [[UIBarButtonItem alloc] initWithCustomView:customToolbar];
self.navigationItem.rightBarButtonItem = customUIBarButtonitem;
}
效果是這樣的
紫色區(qū)和黃色區(qū)是我們無法改變的您机,我當時以為這是最接近神的操作了,然而失敗了年局。
- 還有一種是用自定義view實現际看,效果雖然如出一轍,賊繁瑣矢否,需要重寫btn還需要將view加載到navgationbar上面仲闽,因為原始的navgationbar的背景色是漸變色,那么此時問題來了僵朗,我右滑返回上級viewcontroller赖欣,自定義view并未消失而,你需要讓它消失你要做很多臟活验庙。不痛快
重頭戲來了顶吮,原諒我說了那么多廢話
上代碼
- (void)initNavBar{
// 需要注意為什么兩個btn point都是(0,0)因為設置frame只能控制按鈕的大小
UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
[searchBtn setImage:[UIImage imageNamed:@"spotsupervision_playback_searchbtn"] forState:UIControlStateNormal];
[searchBtn addTarget:self action:@selector(toolbarButtonTapped:) forControlEvents:UIControlEventTouchDown];
searchBtn.tag = 1;
UIButton *uploadBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
[uploadBtn setImage:[UIImage imageNamed:@"spotsupervision_playback_uploadbtn"] forState:UIControlStateNormal];
[uploadBtn addTarget:self action:@selector(toolbarButtonTapped:) forControlEvents:UIControlEventTouchDown];
uploadBtn.tag = 2;
UIBarButtonItem *btn_search = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
UIBarButtonItem *btn_upload = [[UIBarButtonItem alloc] initWithCustomView:uploadBtn];
UIBarButtonItem *negativeRightSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
/**
* width為負數時粪薛,相當于btn向右移動width數值個像素悴了,由于按鈕本身和邊界間距為5pix,所以width設為-5時违寿,間距正好調整
* 為0湃交;width為正數時,正好相反藤巢,相當于往左移動width數值個像素
*/
negativeRightSpacer.width = -5;
UIBarButtonItem *negativeMidSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeMidSpacer.width = 10;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeRightSpacer, btn_upload, negativeMidSpacer, btn_search, nil];
}
不解釋一切盡在代碼中搞莺。