custom titleView of navigationItem is not getting

2019-07-15 17:19发布

I am using a custom titleView and assigning it to navigationItem titleView. It had been working fine until iOS 11. Since the update it's position got misplaced to center as originally it was on more left. Beside that user interaction is not working.

titleView = Bundle.main.loadNibNamed("SomeNib", owner: self, options: nil)?.first as? SomeNib
navigationItem.titleView = titleView

titleView is just a usual nib.

then for enabling interaction:

if let titleView = self.navigationItem.titleView {
            let tap = UITapGestureRecognizer(target: self, action: #selector(onTitleViewTap))
            titleView.addGestureRecognizer(tap)
            titleView.isUserInteractionEnabled = true
        }

标签: ios swift3 ios11
1条回答
虎瘦雄心在
2楼-- · 2019-07-15 18:09

In iOS 11, titleView is getting set with Autolayout. Hence, the size of the titleView is the intrinsic size of the view you are setting in titleView.

This code in your view class(which you are setting as titleView) should help:

override var intrinsicContentSize: CGSize {
    return UILayoutFittingExpandedSize
} 
查看更多
登录 后发表回答