前言
在模擬支付寶的過程中,遇到的第一個問題就是NavBar的實現,一開始打算的是自定義NavBar,但是在push和pop的時候沒有了原版的動畫效果,所以決定用系統自帶的方法完成。接下來講一下過程。
下面是兩者的效果圖。
正文
左側賬單按鈕實現
首先是左邊的賬單按鈕,代碼如下:
UIButton *leftBtn = [UIButton new];
leftBtn.frame = CGRectMake(0, 0, 60, 32);
[leftBtn setImage:[[UIImage imageNamed:@"home_bill"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[leftBtn setTitle:@"賬單" forState:UIControlStateNormal];
leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[leftBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftBarButton;
界面如下:
但是發現兩個問題:
1、圖片和文字考的太近,這個簡單,只需要設置一下按鈕的inset。具體可以看另一篇文章:Button的titleEdgeInsets和imageEdgeInsets
[leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
[leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 0)];
效果蠻好,
2、賬單按鈕太靠右
想讓按鈕靠左,其實也不復雜。代碼:
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
leftSpace.width = - 16.0f;
self.navigationItem.leftBarButtonItems = @[leftSpace, leftBarButton];
大致的解釋一下代碼,其實就是傳建一個width為-16的item來占位,因為寬度為負數,所以leftBarButton不僅不會向右移,反而向左移了。
需要注意的是,這里我設置的width為-16.0f剛好為leftBarButtonItem的x。舉個例子:設置width為-15.0f:
雖然不是很清楚,但是隱約可以看到按鈕左側仍有一個像素的寬度。
好了,左側按鈕完成:
右側兩個按鈕實現
接下來要實現的是右側兩個按鈕,我第一時間想到的就是使用rightBarButtonItems。來試一下:
UIBarButtonItem *rightAddItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"add_tag_reverse"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *rightContactItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"home_contacts"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItems = @[rightAddItem, rightContactItem];
恩,兩個按鈕距離太遠了,我就想到了剛剛賬單按鈕的方法,創建一個占位按鈕,我們來試一下。
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
rightSpace.width = -16.0f;
self.navigationItem.rightBarButtonItems = @[rightAddItem, rightSpace,rightContactItem];
顯然沒什么效果,因為好奇我又換了一下items的順序,把占位item放到最前看看有沒有效果:
self.navigationItem.rightBarButtonItems = @[rightSpace, rightAddItem, rightContactItem];
有效果,這樣看來放在兩個item中間是沒什么效果的。那怎么該怎么實現呢?
其實一開始我也是各種百度各種搜索,但是怎么也找不到我這么細節的問題,后來在賢者時間才想到的??。
解決辦法就是把這兩個按鈕放在一個view上,再設置view為leftItems的其中一個,這樣就可以自由控制按鈕的位置啦。
UIView *btnView = [UIView new];
btnView.frame = CGRectMake(0, 0, 64, 32);
btnView.backgroundColor = [UIColor clearColor];
UIButton *addBtn = [UIButton new];
addBtn.frame = CGRectMake(32, 0, 32, 32);
[addBtn setImage:[UIImage imageNamed:@"add_tag_reverse"] forState:UIControlStateNormal];
[btnView addSubview:addBtn];
UIButton *personBtn = [UIButton new];
personBtn.frame = CGRectMake(0, 0, 32, 32);
[personBtn setImage:[UIImage imageNamed:@"home_contacts"] forState:UIControlStateNormal];
[btnView addSubview:personBtn];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnView];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
rightSpace.width = -16.0f;
self.navigationItem.rightBarButtonItems = @[rightSpace, rightBarButton];
代碼很清楚,就不再說了。
搜索欄實現
首先我們得先了解titleView到底是怎么放的。先做幾個實驗。實驗代碼如下且類似,但是會改變titleView的高度和寬度。
UIView *titleView = [UIView new];
titleView.frame = CGRectMake(0, 0, kWidth, 200);
titleView.backgroundColor = [UIColor greenColor];
self.navigationItem.titleView = titleView;
下面是效果圖:
可以發現,當titleView的寬度大于一定的值后,
titleView的寬就不會再大了,這個值取決于左右側的item的寬度。
知道了這些就有助于設置searchBar了。
searchBar的實現比較簡單,不多說,直接放代碼。
UITextField *searchBar = [UITextField new];
searchBar.frame = CGRectMake(0, 0, kWidth, 28);
searchBar.layer.cornerRadius = 2;
searchBar.font = [UIFont systemFontOfSize:14];
UIImageView *searchBarLeftView = [UIImageView new];
searchBarLeftView.frame = CGRectMake(12, 7, 14, 14);
searchBarLeftView.image = [UIImage imageNamed:@"front_search_icon"];
[searchBar addSubview:searchBarLeftView];
UIImageView *searchBarRightView = [UIImageView new];
searchBarRightView.frame = CGRectMake(160, 6.5, 10, 15);
searchBarRightView.image = [UIImage imageNamed:@"ap_titlebar_search_voice"];
[searchBar addSubview:searchBarRightView];
searchBar.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 0)];
searchBar.leftViewMode = UITextFieldViewModeAlways;
searchBar.backgroundColor = [UIColor colorWithRed:47/255.0 green:141/255.0 blue:214/255.0 alpha:1.0];
searchBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
searchBar.borderStyle = UITextBorderStyleNone;
self.navigationItem.titleView = searchBar;
最后
溫馨提示:能用系統的那就盡量別自定義了啊喂!