【问题标题】:putting labels, buttons on the navigation bar iOS在导航栏上放置标签、按钮 iOS
【发布时间】:2023-03-16 13:10:01
【问题描述】:

我已经创建了自定义导航控制器,

我要添加,左边是日期,右边是返回按钮,标题旁边是返回按钮。

我尝试添加一个标签,但它不起作用。请给我指路

 UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];


UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = @"My Text";

[self.navigationController.navigationBar addSubview:navLabel];

[self.view addSubview:naviBarObj];

【问题讨论】:

  • 请注意,在您的代码中,您试图更改 navigationController.navigationBar 的标题,而不是 naviBarObj

标签: ios uibutton uilabel uinavigationbar uibarbuttonitem


【解决方案1】:
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];

    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
    navLabel.text = @"My Text";
    navLabel.textColor = [UIColor redColor];
    [naviBarObj addSubview:navLabel];
    [navLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:naviBarObj];

试试这个它会工作的。它对我有用:)

【讨论】:

  • 如何使用 Swift 3?
【解决方案2】:

UIToolbar中添加自定义视图

 UIToolbar *tools = [[UIToolbar alloc]
                        initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)]; 

    tools.barStyle = -1; // clear background
    NSMutableArray *buttons = [[NSMutableArray alloc] init];

    UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    bi.width =10.0f;
    [buttons addObject:bi];
    [buttons addObject:add your view];
    bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    bi.width =10.0f;
    [buttons addObject:bi];
    [buttons addObject:add your view];
    [tools setItems:buttons animated:NO];
    UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
    self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem

【讨论】:

    【解决方案3】:

    您也可以通过Interface Builder 实现此目的

    1.首先添加一个UIView,如下所示

    2。然后在UIView 中添加一个UILabel,如下所示

    3.在中心它将如下所示

    现在添加必要的约束以满足您的要求。

    【讨论】:

      【解决方案4】:

      您需要将导航项添加到导航栏。每个导航级别一个。所以你应该至少有一个。

      ** 更新**

      UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:@"Some title"];
      
      /*
       * add buttons and labels
       */
      
      [self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
      

      【讨论】:

      • 无法添加,self.navigationcontroller中没有addSubView方法
      【解决方案5】:

      您需要更改topItemtitleView。像这样:

      naviBarObj.topItem.titleView = navLabel;
      

      ** 更新:完整代码(经过测试和工作)**

      UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
      UINavigationItem *navItem = [[UINavigationItem alloc] init];
      navItem.titleView = titleLbl;
      
      [naviBar pushNavigationItem: navItem animated: NO];
      [navItem release]; // you don't need this for ARC
      
      [self.view addSubview: naviBar];
      [naviBar release]; // you don't need this for ARC
      

      【讨论】:

      • 那么,很可能,您需要在更新之前将 navigationItem 添加到 naviBarObj,就像 @Eimantas 发布的那样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2015-04-06
      • 1970-01-01
      相关资源
      最近更新 更多