定制带有标题视图的导航栏

34

我想在导航栏的中心添加自定义视图,我正在使用以下代码进行测试:

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

我在视图控制器的viewDidLoad方法中进行了设置,但是当我运行程序时,我的导航栏似乎没有任何变化。

你能帮助我解决这个问题吗?


看看这篇文章...适用于iOS4和iOS5的自定义UINavigationBars希望能有所帮助.. :) - Shrey
8个回答

68

这个可行。在初始化时给定框架

UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;

3
是的,那就是它!非常感谢!我可以问一下为什么我无法使用“self.navigationController.navigationItem.titleView”访问导航项吗?从导航控制器访问导航项是否有误?非常感谢@virata。 - Julian Osorio
3
不是关于self.navigationItem.titleViewself.navigationController.navigationItem.titleView的问题,而是关于addSubviewsetTitleView之间的区别。 :) - Kjuly
欢迎,朱利安。navigationItem是你的类的私有属性。不要试图通过navigationController的属性来访问它。 - virata
太好了,我现在明白了!感谢你们两位。 - Julian Osorio
2
奇怪,它在iOS 7上不起作用。我添加了一个搜索栏...然后我收到有关自动布局的投诉.. - Van Du Tran

34

如果您只想为一个视图控制器自定义标题,您可以使用

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;

或者如果您想为所有视图控制器自定义,请使用

[[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]];

您可以将问题 @MusiGenesis 标记为收藏 :) - Raphael Oliveira

27

替换

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

self.navigationItem.titleView = testView;

编辑:

注意:你不能添加子视图到titleView,因为它的默认值是nil,你需要设置一个新的视图作为titleView。


但是你可以将一个子视图添加到 self.navigationController.navigationBar - Kyle Clegg
@KyleClegg 嗯..我不喜欢在导航栏上添加子视图,除了按钮项。;) - Kjuly
@Kjuly同意,我只是想在导航栏中放置一个类似Twitter应用程序的UIPageControl,这就是我能够轻松实现它的方式。根据您想要自定义标题视图的程度,它可能会很有用。 - Kyle Clegg
但是添加titleview不能覆盖整个导航栏。看起来它带有自己的默认插图 :-( - kidsid49

2

Swift 3/4

您可以将UILabel设置为titleView。在viewDidLoad()中调用它:

private func setNavigationTitle(_ title: String) {
    navigationItem.title = nil // clear the default title
    let titleLabel = UILabel() // you don't need to specify a frame, it will be centred in the navbar
    titleLabel.font = ...
    titleLabel.textColor = ...
    titleLabel.text = title
    titleLabel.backgroundColor = .clear
    navigationItem.titleView = titleLabel
    navigationTitleView = titleLabel // you may create a property if you want to manipulate the title view later
}

注意navigationItem.title = nil,否则title可能会覆盖titleView


1
#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

1
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

你好,欢迎来到 Stack Overflow!请编辑你的答案并将代码放入代码块中以使其更易读。可以通过在每行开头放置四个空格或标记代码并按下 {} 按钮来完成此操作。 - Anders

0

您需要使用Storyboard来支持iPhone 6及更高版本(更大的)设备。

为自定义导航项标题/副标题等创建一个容器视图,并将其拖入可视化编辑器中(而不是视图层次结构中)。


0

Swift 3

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

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

另一种备选方案是

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

我建议使用viewDidLoad来设置你的titleView


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接