且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式向导航控制器添加自动布局约束

更新时间:2021-06-29 23:12:49

简而言之:你不能.

导航控制器管理导航栏和可选导航工具栏的创建、配置和显示.允许自定义导航栏与外观相关的属性,但绝不能直接更改其框架、边界或 alpha 值……导航控制器使用导航项对象(UINavigationItem 的实例)动态构建导航栏的内容类)与导航堆栈上的视图控制器相关联

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

来源:Apple Docs

您需要将 UINavigationBar 子类化,然后使用 initWithNavigationBarClass:toolbarClass: 实例化 UINavigationController 以做进一步的事情.

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

如果您正在寻找自定义导航栏的方法,UIKit 用户界面指南 是了解您可以做什么的良好开端.但是,如果它将是一个文本标签,请考虑设置栏的 title 属性,例如.

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.