gpt4 book ai didi

ios - 如何在导航栏中设置父 View 的文本字段全宽

转载 作者:行者123 更新时间:2023-11-28 18:47:07 24 4
gpt4 key购买 nike

我是 iOS 开发新手,我正在尝试开发如下所示的浏览器。

enter image description here

我在代码中设置了 Bar View 的宽度和高度。它工作正常。

enter image description here

然后在pin中加入Url Text Field的约束

enter image description here

宽度不受父容器barView限制(全宽)

enter image description here

我想知道Text Field的宽度有什么特别之处吗?我四处寻找,但没有找到答案。

最佳答案

UINavigationBar 没有正确地向我们公开 Autolayout 引擎。
部分原因是它从 iOS 11 开始支持自动布局!

查看 this gist by Yoshimasa Niwa了解更多信息:

UINavigationBar has been using manual lay outing until iOS 10, so all its content views like titleView has been directly child view of the UINavigationBar. However, since iOS 11, it is using auto layout with bunch of layout guides to lay out its content views in its own internal container view, _UINavigationBarContentView.


一般来说,要对任何 view 施加定位约束,我们需要引用它的 superview 或同级 View 。

someView.leadingAnchor.constraint(equalTo: superView.leadingAnchor)
someView.trailingAnchor.constraint(equalTo: superView.trailingAnchor)

同样,对于 UINavigationBar,我们需要引用 titleView 的父 View ,即 _UINavigationBarContentView

实际上,我们可以遍历 self.navigationController?.navigationBar.subviews 来获取 _UINavigationBarContentView 以将一大堆 anchor 应用于 titleView 但由于 _UINavigationBarContentView 是私有(private)的,因此根本不应引用它。
谁知道层次结构是否会在未来的 iOS 版本中发生变化,或者他们可能会更改名称,或者不知道他们做了什么。

底线:在我看来,现在没有安全的方法可以在 titleView 上应用适当的前导/尾随/顶部/底部/等约束。*
*如果我错了请纠正我


建议:

但是,如果您的 urlTextFieldtitleView 本身而不是 barView,例如:

TextField as TitleView

你可以安全地逃脱:

urlTextField.translatesAutoresizingMaskIntoConstraints = false
urlTextField.widthAnchor.constraint(equalToConstant: self.view.frame.size.width - 16).isActive = true
urlTextField.layoutIfNeeded()

要查看此内容:

Simulator Output

关于ios - 如何在导航栏中设置父 View 的文本字段全宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50595795/

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