Hide the title from UINavigationBar?
Is there any way to hide the ti开发者_如何学运维tle view in a UINavigationBar?
self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectZero];
self.title = @"Home";
Setting to nil will NOT work as described in documentation: If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when the receiver is the top item. If you set this property to a custom title, it is displayed instead of the title.
The titleView is a UIView :
titleView A custom view displayed in the center of the navigation bar when this item is the top item.
@property(nonatomic, retain) UIView *titleView
So I think you can try this :
[titleView setHidden:YES];
Another option which preserves the back button (as answered here: https://stackoverflow.com/a/23113326/1156575):
[self.navigationController.navigationBar setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor clearColor]
}];
For me the solution in swift needed to be in my navigationbar subclass:
self.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.clearColor()]
With the clear color the title disappears. As Josema described, you can also do this by accessing the navigationbar from your navigationcontroller:
self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.clearColor()]
This is what worked for me:
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.clear]
精彩评论