iOS 11 navigationItem.titleView宽度未设置 [英] iOS 11 navigationItem.titleView Width Not Set

查看:616
本文介绍了iOS 11 navigationItem.titleView宽度未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:

还有其他人会遇到吗?

推荐答案

我知道了.我必须重写视图和文本字段的internalContentSize 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

我创建了一个名为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宽度未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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