【问题标题】:Customize navigation bar with title view使用标题视图自定义导航栏
【发布时间】:2012-01-16 00:13:09
【问题描述】:

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

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

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

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

【问题讨论】:

标签: ios objective-c swift uinavigationbar uinavigationitem


【解决方案1】:

替换

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

self.navigationItem.titleView = testView;

编辑:

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

【讨论】:

  • 但是你可以给self.navigationController.navigationBar添加一个子视图。
  • @KyleClegg 嗯.. 我不喜欢在导航栏上添加子视图,按钮项目除外。 ;)
  • @Kjuly 同意,我只是想在类似于 Twitter 应用程序的导航栏中获得UIPageControl,这就是我能够轻松做到的方式。根据您想要自定义标题视图的方式,它可能很有用。
  • 但是添加titleview并没有覆盖整个导航栏。看起来带有自己的默认插图:-(
【解决方案2】:

这行得通。初始化时给框架

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

【讨论】:

  • 是的,就是这样!非常感谢!我可以问你为什么我无法使用以下方式访问导航项:“self.navigationController.navigationItem.titleView”从导航控制器访问导航项是错误的。非常感谢@virata
  • @JulianOsorio 这不是self.navigationItem.titleViewself.navigationController.navigationItem.titleView 的问题,而是addSubviewsetTitleView 的不同。 :)
  • 欢迎 Julian,navigationItem 是您班级自己的财产。不要试图通过navigationController的属性来访问它。
  • 太好了,我现在明白了!谢谢你们两个。
  • 很奇怪,它在 iOS 7 上不起作用.. 我添加了一个搜索栏.. 我收到有关自动布局的投诉..
【解决方案3】:

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

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 :)
【解决方案4】:

您需要使用故事板来支持 iPhone 6 和更新(更大)的设备。

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

【讨论】:

    【解决方案5】:
    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!请编辑您的答案,将代码放入代码块中,使其更具可读性。这可以通过在每行的开头放置四个空格来完成,或者标记代码并按下{} 按钮。
    【解决方案6】:
    #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
    

    【讨论】:

      【解决方案7】:

      斯威夫特 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

      【讨论】:

        【解决方案8】:

        斯威夫特 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多