在 iOS11 上看到一个带有 navigationItem.titleView 的行为,其中 titleView 的宽度不是屏幕的全宽.
Seeing a behavior on iOS11 with a navigationItem.titleView where the width of the titleView is not the full width of the screen.
我有一个自定义视图,我设置为 titleView.在 iOS11 之前,视图将填充导航栏区域.但是 iOS 11 并没有调整大小来填充屏幕的宽度.
I have a custom view that I set as the titleView. Previous to iOS11 the view would fill the navigation bar area. But iOS 11 it is not resizing to fill the width of the screen.
我在设置 titleView 之前尝试设置视图的框架,但没有运气.我也尝试强制 titleViews 超级视图进行布局约束,但没有运气.
I've tried setting the frame of the view before setting titleView but no luck. I've tried to force the titleViews superview to layout constraints as well but no luck.
附上截图:
iOS10:
iOS11:
还有其他人经历过这种情况吗?
Anyone else experience this?
我想通了.我必须覆盖视图和文本字段的 intrinsicContentSize getter.
I figured it out. I had to override the intrinsicContentSize getter for the view, and the text field.
我将宽度设置为 CGFloat.greatestFiniteMagnitude,因此它始终与屏幕一样宽.
I set the width to CGFloat.greatestFiniteMagnitude so it'll always be as wide as the screen.
更新:
由于我已经在这个问题上花费了几个小时,希望其他人能够通过将所有事情紧密结合起来更快地赶上来
Since I've spent couple of hours on this issue, hope that some else will catch up faster by having all things tight up together
我创建了 TitleView
的自定义子类,名为 CustomTitleView
,代码如下:
I've created a custom sub class of TitleView
, called CustomTitleView
, here's the code:
import UIKit
class CustomTitleView: UIView {
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
}
我从一开始就错过的最重要的部分是:
and the most important part which I missed from the start was this:
这篇关于iOS 11 navigationItem.titleView 宽度未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!