iOS 11:一、导航栏变化

一、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<NSAttributedStringKey, id> *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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiaobukuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值