gpt4 book ai didi

ios - 设置 UINavigationItem 的 titleView : setFrame vs. 自动布局约束

转载 作者:行者123 更新时间:2023-11-29 12:39:06 25 4
gpt4 key购买 nike

我正在切换到以编程方式使用自动布局,所以这对我来说是全新的。作为一般规则,我认为我应该避免直接设置任何框架......

问题:我想使用 UILabel 作为我的 View Controller 的 UINavigationItem titleView 的 View 。

是否可以对导航栏/导航项使用自动布局约束?或者,我们是否还需要在某些情况下直接设置框架?

例如我正在做这样的事情:

UILabel* title = [UILabel new];
title.text = @"Some Title";
[title sizeToFit];
CGFloat titleWidth = title.bounds.size.width;
[title setFrame:CGRectMake(0, 0, titleWidth, 32.0)];
self.navigationItem.titleView = title;

如您所见,我将该 View 的高度设置为 32.0(用于横向模式)。此外,我允许更改标题文本,因此如果用户进行了更改,我会重新执行 sizeToFitsetFrame

是这样吗?或者,是否有自动布局方式?

注意:我看过这个问题/答案: Building a titleView programmatically with constraints (or generally constructing a view with constraints)

通读 - 我仍然不确定是否可以使用约束?

最佳答案

还有一个技巧...您设置为 navigationItem.titleView 的 View 不能有 translatesAutoresizingMaskIntoConstraints = false 否则它会在导航动画期间飞过整个导航栏。您还需要确保设置框架宽度和高度。

关于ios - 设置 UINavigationItem 的 titleView : setFrame vs. 自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25505147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com