亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS定制UISearchBar導航欄同步iOS11的方法

 更新時間:2017年12月18日 09:08:43   作者:HasjOH  
本篇文章主要介紹了iOS定制UISearchBar導航欄同步iOS11的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

系統(tǒng)原生的UISearchBar在iOS 11經歷了一次變革,高度由原來的44變成了56 (使用默認高度的估計都被坑了),樣式也發(fā)生了些微的變化,比如在未輸入狀態(tài)下圓角變化,放大鏡圖標和文本的文字不再居中而是靠左了。具體看圖

一些主流App也常見在導航欄嵌入searchBar,以網(wǎng)易云音樂和知乎為例,左邊是主頁,右邊是搜索頁面 (注意光標)。

實現(xiàn)思路與案例

核心思想是設置導航欄的titleView和左右的barButtonItem。主要有3種方式

  1.  首頁導航欄的titleView使用button來實現(xiàn),搜索頁面使用searchBar。
  2. 首頁和搜索頁面導航欄的titleView都是用searchBar,searchBar的樣式針對兩個頁面做不同的修改。這種方式可以重用我們定制的searchBar,減少冗余。
  3. 首頁導航欄titleView使用button來實現(xiàn),搜索頁面的使用textField。這種方式更徹底,更靈活,相對也更復雜一些。

為什么上面的titleView說是button不是其他的?其他的當然也可以實現(xiàn)。button自帶imageView和titleLabel,只需要設置偏移量更容易達到我們想要的,而且視圖層級更少,在流暢性方面更有保證些。

案例

網(wǎng)易云音樂首頁和搜索頁面的導航欄視圖層級,titleView都使用MCSearchBar來實現(xiàn),并且設置了導航欄左右兩邊的按鈕 。這類似上文所說的第二種思路。

 

圖中可以清楚看到知乎首頁導航欄由2個button組成,搜索頁面使用了textField,這類似上文提到的第三種思路。

實戰(zhàn)

通過自定義SearchBar實現(xiàn)一個如下樣式的導航欄

先自定義一個UISearchBar的初始化方法,觀察一下首頁和搜索頁的異同,像searchField的大小背景色是一致的,可以這部分可以直接給定,而placeholder是不一樣的,所以應該在調用的時候提供。以此類推,新建一個OHSearchBar類,一個初始化方法

- (instancetype)initWithFrame:(CGRect)frame placeholder:(NSString *)placeholder textFieldLeftView:(UIImageView *)leftView showCancelButton:(BOOL)showCancelButton tintColor:(UIColor *)tintColor { 
 if (self = [super initWithFrame:frame]) {
  self.frame = frame;
  self.tintColor = tintColor; //光標顏色
  self.barTintColor = [UIColor whiteColor];
  self.placeholder = placeholder;
  self.showsCancelButton = showCancelButton;
  self.leftView = leftView; // 用來代替左邊的放大鏡
  [self setImage:[UIImage imageNamed:@"clear"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal]; // 替換輸入過程中右側的clearIcon
 }
 return self;
}

新建一個首頁OHHomeViewController,設置導航欄的titleView和rightBarButton

// navigation buttom
 UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeSystem];
 [messageButton setImage:[UIImage imageNamed:@"msg"] forState:UIControlStateNormal];
 messageButton.bounds = CGRectMake(0, 0, 30, 30);
 UIBarButtonItem *messageBarButton = [[UIBarButtonItem alloc] initWithCustomView:messageButton];
 self.navigationItem.rightBarButtonItem = messageBarButton;
 
 // search bar
 UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"scan"]];
 leftView.bounds = CGRectMake(0, 0, 24, 24);
 self.ohSearchBar = [[OHSearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)
            placeholder:@"點擊我跳轉"
          textFieldLeftView:leftView
           showCancelButton:NO
            tintColor:[UIColor clearColor]];
 self.navigationItem.titleView = self.ohSearchBar;

讓我們來看下效果,左邊為iOS 9,右邊iOS 11

這時候可以看到幾處差異

  1. searchBar的高度
  2. searchBar的textField的放大鏡和文字位置
  3. textField的圓角不一致
  4. 更細心的還會發(fā)現(xiàn),textField的位置不一致

解決方法: 第一和第二個問題,判斷設備是否是iOS 11,若是則設置其高度,不是則讓其placeholder居左。關鍵代碼如下

if ([[UIDevice currentDevice] systemVersion].doubleValue >= 11.0) {
  [[self.heightAnchor constraintEqualToConstant:44.0] setActive:YES];
 } else {
  [self setLeftPlaceholder];
 }
- (void)setLeftPlaceholder {
 SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);
 if ([self respondsToSelector:centerSelector]) {
  BOOL centeredPlaceholder = NO;
  NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
  NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  [invocation setTarget:self];
  [invocation setSelector:centerSelector];
  [invocation setArgument:&centeredPlaceholder atIndex:2];
  [invocation invoke];
 }
}

對于第三和第四個問題,用KVC獲取textField,并對其進行定制。令textField位置、大小、圓角一致。

- (void)layoutSubviews{
 [super layoutSubviews];
 // search field
 UITextField *searchField = [self valueForKey:@"searchField"];
 searchField.backgroundColor = DARK_BLUE_COLOR;
 searchField.textColor = [UIColor whiteColor];
 searchField.font = [UIFont systemFontOfSize:16];
 searchField.leftView = self.leftView;
 searchField.frame = CGRectMake(0, 8, SCREEN_WIDTH, 28);
 searchField.layer.cornerRadius = 5;
 searchField.layer.masksToBounds = YES;
 [searchField setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
 [self setValue:searchField forKey:@"searchField"]; 
 self.searchTextPositionAdjustment = (UIOffset){10, 0}; // 光標偏移量
}

同樣的,先看下運行效果

原本以為這下是沒什么問題的,結果簡直是坑

textFild的長度、位置、圓角都不一樣 解釋下這里出現(xiàn)的問題

觀察上方圖片上方的searchBar,會發(fā)現(xiàn)textField左邊是圓角,右邊是直角,說明是被截取的。導航欄titleView的范圍就劃分到了那個部分,而下邊的searchBar連rightBarButton都不放過,直接搶占了位置。推測這是由于iOS 11導航欄視圖層級變化產生的,可以這里了解下 www.jianshu.com/p/352f101d6… ,或者自行科普,不詳細展開。所以對于searchBar的size設置要小心了,盡量控制在合適的范圍。

textField的圓角是不一致的,自定義圓角大小時,取消其本身的圓角樣式

searchField.borderStyle = UITextBorderStyleNone;

查看視圖層級會發(fā)現(xiàn),iOS 11以下,設置titleView,x的默認坐標居然是12,而iOS 11是0。所以設置textField的x坐標的話,在iOS 11下必須多出12才會是一致的位置。

 

修改代碼上面的代碼

- (void)layoutSubviews{
 [super layoutSubviews];
 // search field
 UITextField *searchField = [self valueForKey:@"searchField"];
 searchField.backgroundColor = DARK_BLUE_COLOR;
 searchField.textColor = [UIColor whiteColor];
 searchField.font = [UIFont systemFontOfSize:16];
 searchField.leftView = self.leftView;
 if (@available(iOS 11.0, *)) {
  // 查看視圖層級,在iOS 11之前searchbar的x是12
  searchField.frame = CGRectMake(12, 8, SCREEN_WIDTH*0.8, 28);

 } else {
  searchField.frame = CGRectMake(0, 8, SCREEN_WIDTH*0.8, 28);
 }
 searchField.borderStyle = UITextBorderStyleNone;
 searchField.layer.cornerRadius = 5;
 searchField.layer.masksToBounds = YES;
 [searchField setValue:[UIColor whiteColor] forKeyPath:@"placeholderLabel.textColor"];
 [self setValue:searchField forKey:@"searchField"];
  self.searchTextPositionAdjustment = (UIOffset){10, 0}; // 光標偏移量
}

這時候就是我們想要的結果了。

首頁暫時告一段落,接著開始我們的搜索頁面。與首頁不同的是需要searchBar與searchController配合使用。新建一個OHSearchController類 添加一個屬性

@property (nonatomic, strong) OHSearchBar *ohSearchBar;

初始化代碼

- (instancetype)initWithSearchResultsController:(UIViewController *)searchResultsController searchBarFrame:(CGRect)searchBarFrame placeholder:(NSString *)placeholder textFieldLeftView:(UIImageView *)leftView showCancelButton:(BOOL)showCancelButton barTintColor:(UIColor *)barTintColor{
 if (self = [super initWithSearchResultsController:searchResultsController]) {
  self.ohSearchBar = [[OHSearchBar alloc] initWithFrame:searchBarFrame
             placeholder:placeholder
           textFieldLeftView:leftView
            showCancelButton:YES
             tintColor:barTintColor];
  
  UIButton *button = [self.ohSearchBar valueForKey:@"cancelButton"];
  button.tintColor = [UIColor whiteColor];
  [button setTitle:@"取消" forState:UIControlStateNormal];
  [self.ohSearchBar setValue:button forKey:@"cancelButton"];
 }
 return self;
}

接著是我們的視圖控制器OHSearchViewController

UIImageView *leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
 leftView.bounds = CGRectMake(0, 0, 24, 24);
 self.ohSearchController = [[OHSearchController alloc] initWithSearchResultsController:self
                   searchBarFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)
                    placeholder:@"請輸入搜索內容進行搜索"
                  textFieldLeftView:leftView
                   showCancelButton:YES
                    barTintColor:BASE_BLUE_COLOR];
 
 [self.ohSearchController.ohSearchBar becomeFirstResponder];
 self.ohSearchController.ohSearchBar.delegate = self;
 [self.ohSearchController.ohSearchBar setLeftPlaceholder];
 self.navigationItem.titleView = self.ohSearchController.ohSearchBar;
 self.navigationItem.hidesBackButton = YES;

完成這一步后到了交互環(huán)節(jié)了,點擊首頁的searchBar跳轉搜索頁面,點擊搜索頁面的取消按鈕返回到首頁。 首頁設置searchbar的代理,并完成一下代理方法

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
 OHSearchViewController *ohSearchViewController = [[OHSearchViewController alloc] init];
 [self.navigationController pushViewController:ohSearchViewController animated:NO];
 return YES;
}

搜索頁設置searchbar的代理,并完成一下代理方法

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
 [self.navigationController popViewControllerAnimated:NO];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
 [self.ohSearchController.ohSearchBar resignFirstResponder];
 // 讓取消按鈕一直處于激活狀態(tài)
 UIButton *cancelBtn = [searchBar valueForKey:@"cancelButton"];
 cancelBtn.enabled = YES;
}

這時候問題又出現(xiàn)了,點擊搜索頁面的取消按鈕,沒有跳回首頁而是還在這個頁面。但是可以看到屏幕的閃動。通過打印消息發(fā)現(xiàn),點了取消按鈕,執(zhí)行了首頁的- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar方法。 仔細推敲之后想明白了原因是沒有取消第一響應者,加上導航欄的交互機制,pop到上個頁面的時候并不會進行頁面刷新導致了這個問題。 解決辦法在首頁要push搜索頁面的時候取消第一響應者

- (void)viewWillDisappear:(BOOL)animated {
 [self.ohSearchBar resignFirstResponder];
}

到此,便大功告成了??梢钥聪略创a加深理解。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 你應該知道的tableViewCell行高計算處理

    你應該知道的tableViewCell行高計算處理

    這篇文章主要給大家介紹了關于tableViewCell行高計算的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • 關于ios配置微信config出現(xiàn)驗簽失敗的問題解決

    關于ios配置微信config出現(xiàn)驗簽失敗的問題解決

    這篇文章主要介紹了關于ios配置微信config出現(xiàn)驗簽失敗的問題解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • iOS正則表達式驗證手機號、郵箱、身份證號等

    iOS正則表達式驗證手機號、郵箱、身份證號等

    這篇文章主要介紹了iOS正則表達式驗證手機號、郵箱、身份證號等信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • iOS Swift開發(fā)之日歷插件開發(fā)示例

    iOS Swift開發(fā)之日歷插件開發(fā)示例

    本篇文章主要介紹了iOS Swift開發(fā)之日歷插件開發(fā)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • iOS中Label實現(xiàn)顯示不同顏色與字體的方法

    iOS中Label實現(xiàn)顯示不同顏色與字體的方法

    這篇文章主要給大家介紹了關于在iOS中Label實現(xiàn)顯示不同顏色與字體的相關資料,文中分別介紹了利用range或者文字兩種實現(xiàn)的方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-11-11
  • iOS實現(xiàn)帶文字的圓形頭像效果

    iOS實現(xiàn)帶文字的圓形頭像效果

    隨著騰訊QQ的普及,現(xiàn)在越來越多的社交類APP在顯示頭像的時候,都選擇了圓形頭像,本文將更進一步的介紹如何實現(xiàn)帶文字的圓形頭像效果,效果非常不錯,感興趣的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • iOS開發(fā)學習 ViewController使用示例詳解

    iOS開發(fā)學習 ViewController使用示例詳解

    這篇文章主要為大家介紹了iOS開發(fā)學習 ViewController使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • iOS?Lotusoot模塊化工具應用的動態(tài)思路

    iOS?Lotusoot模塊化工具應用的動態(tài)思路

    項目的不斷更迭,導致項目越來越大,越來越臃腫,為了讓項目更加條理,需要對項目進行模塊化處理,為了減少模塊之間的耦合,于是就有了Lotusoot這個工具
    2022-08-08
  • IOS打開系統(tǒng)相機的閃光燈

    IOS打開系統(tǒng)相機的閃光燈

    今天給大家分享一下如何調用iphone的拍照功能和打開閃光燈,有些代碼我也不太理解,很多是在網(wǎng)上借鑒其他人的。
    2015-05-05
  • iOS實現(xiàn)H5支付(微信、支付寶)原生封裝

    iOS實現(xiàn)H5支付(微信、支付寶)原生封裝

    這篇文章主要介紹了iOS實現(xiàn)H5支付(微信、支付寶)原生封裝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02

最新評論