Customize navigation bar with title view

2019-01-22 06:15发布

I am trying to add a custom view in the center of a navigation bar and I am using the following code to test it:

UIView * testView = [[UIView alloc] init];
[testView setBackgroundColor:[UIColor blackColor]];
testView.frame = CGRectMake(0, 0, 100, 35);
[self.navigationController.navigationItem.titleView addSubview:testView];

I am setting this up in the viewDidLoad method of my view controller but when i run my program nothing seems to change in my navigation bar.

Could you help me with this?

8条回答
可以哭但决不认输i
2楼-- · 2019-01-22 06:56

If you want to just customize the title for one view controller you can use

UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = @"Diga-nos o motivo";
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
lblTitle.shadowColor = [UIColor whiteColor];
lblTitle.shadowOffset = CGSizeMake(0, 1);
lblTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0];
[lblTitle sizeToFit];

self.navigationItem.titleView = lblTitle;

or if you want to customize for all view controllers use

[[UINavigationBar appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
        UITextAttributeTextColor, 
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
        UITextAttributeTextShadowColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
        UITextAttributeTextShadowOffset, 
        [UIFont fontWithName:@"Arial-Bold" size:10.0], 
        UITextAttributeFont, 
        nil]];
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-22 07:03

Replace

[self.navigationController.navigationItem.titleView addSubview:testView];

to

self.navigationItem.titleView = testView;

Edit:

Note: You cannot add subviews to titleView cause it's default value is nil, you need to set a new view as the titleView.

查看更多
老娘就宠你
4楼-- · 2019-01-22 07:03
CustomLabel *titleLabel = [CustomLabel initWithLabelFrame:labelFrame textFont:[UIFont fontWithName:@"Helvetica" size:[UIFont systemFontSize]] textColor:[UIColor blackColor] labelText:@"Add as" textAlignment:NSTextAlignmentCenter labelOnView:reference.view labelTag:62];

[self.navigationItem setTitleView:titleLabel]; // titleLabel set in navigationItem
查看更多
Explosion°爆炸
5楼-- · 2019-01-22 07:03

Swift 3

let myImageView = UIImageView(image: <...set your image...>)

override fun viewDidLoad(){
   super.viewDidLoad()
   self.navigationItem.titleView = myImageView  //
}

One more alternate solution is

override fun viewWillAppear(_ animated: Bool) {
   super. viewWillAppear(animated)
   self.navigationItem.titleView = myImageView
}

I recommend to use, viewDidLoad to setup your titleView

查看更多
Lonely孤独者°
6楼-- · 2019-01-22 07:04
#import <UIKit/UIKit.h> 
@interface MasterViewController : UITableViewController
@end


#import "MasterViewController.h"
@interface MasterViewController ()
@end

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.titleView = [self titleView];
}

- (UIView *)titleView {
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat width = 0.95 * self.view.frame.size.width;
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, navBarHeight)];

    UIImage *logo = [UIImage imageNamed:@"logo.png"];
    UIButton *logoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat logoY = floorf((navBarHeight - logo.size.height) / 2.0f);
    [logoButton setFrame:CGRectMake(0, logoY, logo.size.width, logo.size.height)];
    [logoButton setImage:logo forState:UIControlStateNormal];

    UIImage *bubble = [UIImage imageNamed:@"notification-bubble-empty.png"];
    UIImageView *bubbleView = [[UIImageView alloc] initWithImage:bubble];

    const CGFloat Padding = 5.0f;
    CGFloat bubbleX = 
        logoButton.frame.size.width + 
        logoButton.frame.origin.x + 
        Padding;
    CGFloat bubbleY = floorf((navBarHeight - bubble.size.height) / 2.0f);
    CGRect bubbleRect = bubbleView.frame;
    bubbleRect.origin.x = bubbleX;
    bubbleRect.origin.y = bubbleY;
    bubbleView.frame = bubbleRect;

    [containerView addSubview:logoButton];
    [containerView addSubview:bubbleView];

    return containerView;
}

@end
查看更多
小情绪 Triste *
7楼-- · 2019-01-22 07:10

You'll want to use storyboard to do this to support iPhone 6 and newer (larger) devices.

Create a container view for your custom navigation item title/subtitle etc, and drag it into the visual editor (not into the view hierarchy).

查看更多
登录 后发表回答