[英]UISplitViewController: titleView in DetailViewController disappears on landscape orientation, intended behaviour?
I'm adding a custom titleView
inside a navigation bar using navigationItem.titleView
for both Master/Detail VC.我正在使用
navigationItem.titleView
为 Master/Detail VC 在导航栏中添加自定义titleView
。 On changing the orientation of the device to landscape, titleView
under MasterViewController
works fine, but for DetailViewController
titleView
disappears.在将设备的方向更改为横向时,
MasterViewController
下的titleView
工作正常,但对于DetailViewController
titleView
消失了。 On changing the orientation back to portrait titleView
appears back for DetailViewController
.在将方向改回纵向时,
titleView
出现在DetailViewController
。 I have also attached a link for source code and video.我还附上了源代码和视频的链接。
Is this an intended behavior or am I making a mistake from my side or is it an issue from Apple's side ?这是有意的行为还是我犯了错误,还是苹果方面的问题?
//Custom Title View:
class TitleView: UIView {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: 50, height: 20)
}
}
class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Adding titleView for Master/Detail VC:
navigationItem.titleView = {
//Setting frame size here, did not make any difference
let view = TitleView(frame: .zero)
view.backgroundColor = .red
return view
}()
}
}
Full source code here: https://github.com/avitron01/SplitViewControllerIssue/tree/master/MathMonsters完整源代码在这里: https : //github.com/avitron01/SplitViewControllerIssue/tree/master/MathMonsters
Video highlighting the issue: https://vimeo.com/336288580突出问题的视频: https : //vimeo.com/336288580
I had the same issue.我遇到过同样的问题。 It seems an iOS bug.
这似乎是一个iOS错误。 My workaround was to reassign the title view on every view layout.
我的解决方法是在每个视图布局上重新分配标题视图。 I used this piece of code in my
DetailViewController
:我在我的
DetailViewController
使用了这段代码:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let v = navigationItem.titleView {
navigationItem.titleView = nil
navigationItem.titleView = v
}
}
For those who stumble upon this, see also iOS 11 navigationItem.titleView Width Not Set .对于那些偶然发现这一点的人,另请参阅iOS 11 navigationItem.titleView Width Not Set 。 Basically, there's two suggested workarounds:
基本上,有两种建议的解决方法:
intrinsicContentSize
to be as big as possible with UIView.layoutFittingExpandedSizeintrinsicContentSize
处理得尽可能大widthAnchor
/ heightAnchor
constraints to set width and height of your viewwidthAnchor
/ heightAnchor
约束来设置视图的宽度和高度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.