【问题标题】:Cannot modify constraints for UIToolbar managed by a UINavigationController无法修改由 UINavigationController 管理的 UIToolbar 的约束
【发布时间】:2023-04-01 14:55:01
【问题描述】:

我将 NSContraints 添加到 UINavigationController 的工具栏中,我收到此错误:

无法修改由UINavigationController 管理的UIToolbar 的约束

In ViewDidLoad:
  self.navigationController.toolbarHidden = NO;
[self.navigationController.toolbar addSubview:_labelName];
[self.navigationController.toolbar addSubview:_labelAddress];

然后我调用下面的方法:

NSLayoutConstraint *nameRight = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeRight multiplier:1 constant:20];
NSLayoutConstraint *nameLeft = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
NSLayoutConstraint *nameTop = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeTop multiplier:1 constant:0];
NSLayoutConstraint *nameHeight = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeHeight multiplier:.5 constant:0];

NSLayoutConstraint *addressRight = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeRight multiplier:1 constant:20];
NSLayoutConstraint *addressLeft = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
NSLayoutConstraint *addressTop = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_labelName attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
NSLayoutConstraint *addressHeight = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.navigationController.toolbar attribute:NSLayoutAttributeHeight multiplier:.5 constant:0];


[self.navigationController.toolbar addConstraints:@[nameRight, nameLeft, nameTop, nameHeight, addressRight, addressLeft, addressTop, addressHeight]];

我应该在导航控制器的工具栏中创建一个UIView 吗?并将约束放入其中?

【问题讨论】:

    标签: ios xcode uinavigationcontroller nslayoutconstraint


    【解决方案1】:

    自动布局约束仅适用于 UIView 及其子类。 您的替代方法是使用仅基于 UIViews 的小部件来滚动您自己的工具栏。

    链接:iOS Autolayout and UIToolbar/UIBarButtonItems

    希望这能解决您的问题。

    【讨论】:

      【解决方案2】:

      问题是您需要将工具栏项添加到 [self using setToolBarItem] 而不是 [self.navigationController.toolbar ..] 即使从技术上讲它是同一个对象。

      和self.title一样,设置导航控制器的标题,[self setToolbarItems设置导航控制器工具栏的工具栏项

      [self setToolbarItems:@[barButtonItem] animated:NO];  <-- Answer
      
      [self.navigationController.toolbar setItems:@[barButtonItem]]; <-- BAD
      

      对于 NSLayout,您可以在 BarButtonItem 的视图中执行此操作:

      _toolBarView = [[UIView alloc] init];
      [_toolBarView addSubview:_labelName];
      [_toolBarView addSubview:_labelAddress];
      
      barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_toolBarView];
      

      NSLayout 代码:

      NSLayoutConstraint *nameRight = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
      NSLayoutConstraint *nameLeft = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
      NSLayoutConstraint *nameTop = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
      NSLayoutConstraint *nameHeight = [NSLayoutConstraint constraintWithItem:_labelName attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0];
      
      NSLayoutConstraint *addressRight = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
      NSLayoutConstraint *addressLeft = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
      NSLayoutConstraint *addressTop = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_labelName attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
      NSLayoutConstraint *addressHeight = [NSLayoutConstraint constraintWithItem:_labelAddress attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_toolBarView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0];
      
      [_toolBarView addConstraints:@[nameRight, nameLeft, nameTop, nameHeight, addressRight, addressLeft, addressTop, addressHeight]];
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2017-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 2016-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多