iOS 11:一、导航栏变化
发布日期:2021-05-11 01:28:39 浏览次数:12 分类:精选文章

本文共 4098 字,大约阅读时间需要 13 分钟。

一、prefersLargeTitles属性

iOS11 UINavigationBar新添加的prefersLargeTitles属性

1、prefersLargeTitles

/// When set to YES, the navigation bar will use a larger out-of-line title view when requested by the current navigation item. To specify when the large out-of-line title view appears, see UINavigationItem.largeTitleDisplayMode. Defaults to NO.@property (nonatomic, readwrite, assign) BOOL prefersLargeTitles UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

大标题,默认为false,当设置为true时,navigation bar会显示大标题,标题显示在左边,如果页面有scrollview控件或父控件为这个时,当向上滑动页面,标题会变小直到跟之前显示一样,同时标题位置也会发生变化。

  • 当这个属性设置为NO的时候,navigation bar 的高度为44(iPhone X除外);
  • 当这个属性设置为NO的时候,navigation bar 的高度为96(iPhone X除外);

2、largeTitleDisplayMode

/// When UINavigationBar.prefersLargeTitles=YES, this property controls when the larger out-of-line title is displayed. If prefersLargeTitles=NO, this property has no effect. The default value is Automatic.@property (nonatomic, readwrite, assign) UINavigationItemLargeTitleDisplayMode largeTitleDisplayMode API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

该属性包含四个值

typedef NS_ENUM(NSInteger, UINavigationItemLargeTitleDisplayMode) {    /// Automatically use the large out-of-line title based on the state of the previous item in the navigation bar. An item with largeTitleDisplayMode=Automatic will show or hide the large title based on the request of the previous navigation item. If the first item pushed is set to Automatic, then it will show the large title if the navigation bar has prefersLargeTitles=YES.    UINavigationItemLargeTitleDisplayModeAutomatic,    /// Always use a larger title when this item is top most.    UINavigationItemLargeTitleDisplayModeAlways,    /// Never use a larger title when this item is top most.    UINavigationItemLargeTitleDisplayModeNever,} NS_SWIFT_NAME(UINavigationItem.LargeTitleDisplayMode);

largeTitleDisplayMode是配合prefersLargeTitles属性的,只要当prefersLargeTitles为YES时才生效,largeTitleDisplayMode有三个模式:

  • UINavigationItemLargeTitleDisplayModeAutomatic:自动显示大标题或小标题。用我的话来说:初始时是大标题,当滑动使大标题隐藏时显示小标题。
  • UINavigationItemLargeTitleDisplayModeAlways:总是显示大标题。
  • UINavigationItemLargeTitleDisplayModeNever:总是显示小标题。

3、largeTitleTextAttributes

/* You may specify the font, text color, and shadow properties for the large title in the text attributes dictionary, using the keys found in NSAttributedString.h. */@property(nullable, nonatomic, copy) NSDictionary
*largeTitleTextAttributes UI_APPEARANCE_SELECTOR API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

设置标题属性(颜色,字体大小)

[self.navigationController.navigationBar setLargeTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:18.0f],NSFontAttributeName,nil]];

二、导航栏视图的位置变更

1、iOS 11 以前

这里写图片描述

navigationBarButton则直接添加在navigationBar上面

2、iOS 11 以后

这里写图片描述

titleView直接加在_UINavigationBarContentView上,UIBarButtonItem则添加在_UIButtonBarStackView上面,_UIButtonBarStackView则添加在_UINavigationBarContentView上面,最后添加到UINavigationBar上面。

三、UISearchController

UISearchController控件是iOS 8时引入的,经常配合UITableView的tableHeaderView使用,在iOS 11 UISearchController使用在navigationItem添加了两个属性 。

// A view controller that will be shown inside of a navigation controller can assign a UISearchController to this property to display the search controller’s search bar in its containing navigation controller’s navigation bar.@property (nonatomic, retain, nullable) UISearchController *searchController API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);// If this property is true (the default), the searchController’s search bar will hide as the user scrolls in the top view controller’s scroll view. If false, the search bar will remain visible and pinned underneath the navigation bar.@property (nonatomic) BOOL hidesSearchBarWhenScrolling API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);

使用方式变换:

UISearchController *searchC = [[UISearchController alloc]initWithSearchResultsController:nil];

if (@available(iOS 11.0, *)) {	self.navigationItem.searchController = searchC;	self.navigationItem.hidesSearchBarWhenScrolling = NO;} else {	self.tableView.tableHeaderView = searchC.searchBar;}
上一篇:iOS 11:二、UIScrollView偏移
下一篇:MAC构建ipv6环境

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月02日 03时14分46秒