【问题标题】:Add 2 UILabels in UINavigationBar titleView on top of one another在 UINavigationBar titleView 中添加 2 个 UILabels
【发布时间】:2013-01-29 12:09:57
【问题描述】:

我有一个自定义 UINavigationBar,在其 titleView 中有一个 2 行标签:

        UILabel *navBarLabel = [[UILabel alloc] initWithFrame:CGRectZero];  
        UINavigationItem *item = [[UINavigationItem alloc] init];  
        navBarLabel.backgroundColor = [UIColor clearColor];
        navBarLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
        navBarLabel.numberOfLines = 2;
        navBarLabel.textAlignment = UITextAlignmentCenter;
        navBarLabel.textColor = [UIColor colorWithRed:124.0/255.f green:125.0/255.f blue:128.0/255.f alpha:1.0];
        navBarLabel.text = @"This\nis an example";
        [navBarLabel sizeToFit];
        item.titleView = navBarLabel;

但是我不想让我的UILabel 包含 2 行文本,我想添加 2 个UILabels一个在另一个之上,以实现每个字体的自定义'线'分开。它是如何实施的?有什么想法吗?

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch uinavigationbar


    【解决方案1】:

    只需创建一个父容器UIView 来保存您的两个标签,并将此父容器用作您的标题视图。在伪代码中:

       UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
       [titleView addSubview:someLabel];
       [titleView addSubview:anotherLabel];
       item.titleView = titleView;
    

    ...您需要确保正确设置标签框架,以便一个标签位于另一个标签之上。具体操作方式取决于您是否使用自动布局,但它非常简单明了。

    【讨论】:

      【解决方案2】:

      iOS 允许您在栏中添加两个项目。试试下面的代码。

        UIButton *tempButton = nil;
          UIImage  *tempButtonImage = nil;
          UIImage  *tempButtonPressedImage = nil;
      
          tempButtonImage = [UIImage imageNamed:@"unselected1.png"];
          tempButtonPressedImage = [UIImage imageNamed:@"selected1.png"];
          tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
          [tempButton setImage : tempButtonImage forState : UIControlStateNormal];
          [tempButton setImage : tempButtonPressedImage forState : UIControlStateHighlighted];
          [tempButton addTarget : self action : @selector(tempButtonClick:) forControlEvents : UIControlEventTouchUpInside];
          tempButton.frame = CGRectMake(0, 0, tempButtonImage.size.width, tempButtonImage.size.height);
      
          UIView *tempButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, tempButtonImage.size.width, tempButtonImage.size.height}];
          tempButtonContainer.backgroundColor = [UIColor clearColor];
          [tempButtonContainer addSubview:tempButton];
      
          UIBarButtonItem *tempToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:tempButtonContainer];
          // custom logout button
          UIButton *temp2Button = nil;
          UIImage  *temp2ButtonImage = nil;
          UIImage  *temp2ButtonPressedImage = nil;
      
          temp2ButtonImage = [UIImage imageNamed:@"unselectedtemp2Button.png"];
          temp2ButtonPressedImage = [UIImage imageNamed:@"selectedtemp2Button.png"];
          temp2Button = [UIButton buttonWithType:UIButtonTypeCustom];
          [temp2Button setImage : temp2ButtonImage forState : UIControlStateNormal];
          [temp2Button setImage : temp2ButtonPressedImage forState : UIControlStateHighlighted];
          [temp2Button addTarget : self action : @selector(temp2ButtonClick:) forControlEvents : UIControlEventTouchUpInside];
          temp2Button.frame = CGRectMake(0, 0, temp2ButtonImage.size.width, temp2ButtonImage.size.height);
      
          UIView *temp2ButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, temp2ButtonImage.size.width, temp2ButtonImage.size.height}];
          temp2ButtonContainer.backgroundColor = [UIColor clearColor];
          [temp2ButtonContainer addSubview:temp2Button];
      
          UIBarButtonItem *temp2ToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:temp2ButtonContainer];
      
          // setting right items
          self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:tempToolbarButton, temp2ToolbarButton, nil];
      
      
          UIButton *button = nil;
          UIImage  *buttonImage = nil;
          UIImage  *buttonPressedImage = nil;
      
          buttonImage = [UIImage imageNamed:@"asd.png"];
          buttonPressedImage = [UIImage imageNamed:@"selected-asd.png"];
          button = [UIButton buttonWithType:UIButtonTypeCustom];
          [button setImage : buttonImage forState : UIControlStateNormal];
          [button setImage : buttonPressedImage forState : UIControlStateHighlighted];
          [button addTarget : self action : @selector(buttonClick:) forControlEvents : UIControlEventTouchUpInside];
          button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
      
          UIView *buttonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, buttonImage.size.width, buttonImage.size.height}];
          buttonContainer.backgroundColor = [UIColor clearColor];
          [buttonContainer addSubview:button];
      
          UIBarButtonItem *toolbarButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
      
          self.navigationItem.leftBarButtonItem = toolbarButton;
      

      【讨论】:

      • 抱歉,我不需要按钮,我需要标签
      • 使用标签而不是按钮。
      猜你喜欢
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多