添加自动布局约束导航​​控制器编程 [英] Adding autolayout constraints to navigation controller programatically

查看:686
本文介绍了添加自动布局约束导航​​控制器编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制添加到我的导航栏上添加了UI标签。在UI标签显示正在运行的计时器

I am trying to add constraints to a UI label I added on a navigation bar. The UI label shows a running timer

下面是我的code,

self.label = [[UILabel alloc] initWithFrame:CGRectMake(550, 15, 150, 14)];
self.label.text = @"Label";
[self.label setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.navigationController.navigationBar addSubview:self.label];

[self.navigationController.navigationBar addConstraint:[NSLayoutConstraint constraintWithItem:self.label
                                                       attribute:NSLayoutAttributeTrailing
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.navigationController.navigationBar
                                                       attribute:NSLayoutAttributeTrailing
                                                      multiplier:1.0f
                                                        constant:-20.0f]];

但我得到的错误无法修改由一个控制器管理的UINavigationBar的约束当我运行的应用程序。

But I get the error 'Cannot modify constraints for UINavigationBar managed by a controller' when I run the app.

如何添加此约束?

推荐答案

在短期:你不能。

导航控制器管理创建,配置和导航栏和可选的导航工具栏的显示。它允许自定义导航栏的外观相关的属性,但动态地使用导航项目对象(UINavigationItem的情况下,您不会直接改变它的框架,边界或Alpha值...导航控制器建立导航栏的内容类)与导航堆栈上的视图控制器关联

The navigation controller manages the creation, configuration, and display of the navigation bar and optional navigation toolbar. It is permissible to customize the navigation bar’s appearance-related properties but you must never change its frame, bounds, or alpha values directly ... A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack

<一个href=\"https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/\"相对=nofollow>来源:苹果文档

您将需要继承的 UINavigationBar的,然后实例化的UINavigationController 使用 initWithNavigationBarClass: toolbarClass:来进一步做一些事情。

You would need to subclass the UINavigationBar and then instantiate the UINavigationController using initWithNavigationBarClass:toolbarClass: to do something further.

但如果你正在寻找方法来定制导航栏,<一个href=\"https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UINavigationBar.html\"相对=nofollow> UIKit的用户界面指南是一个好的开始知道你可能能做什么。但是,如果它要成为一个文本标签,考虑设置栏中的标题属性,例如。

Though if you're looking for ways to customize the navbar, UIKit User Interface Guide is a good start to know what you may be able to do. But if it's going to be a text label, consider setting the bar's title property, for example.

这篇关于添加自动布局约束导航​​控制器编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆