一、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;}