custom titleView of navigationItem is not getting tapped on iOS 11

隐身守侯 提交于 2020-01-24 13:02:31

问题


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
        }

回答1:


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
} 


来源:https://stackoverflow.com/questions/46461350/custom-titleview-of-navigationitem-is-not-getting-tapped-on-ios-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!