iOS11自定义导航栏标题,位置有偏差并且无法响应点击事件和手势的解决办法(Swift)
2018-01-04 15:12
1341 查看
问题描述:最近项目中需要自定义导航栏标题,但是在iOS11+Xcode9下,自定义导航栏标题总是有问题:1.是位置不好控制,总会有偏差。2.是无法响应点击事件和手势。
产生问题的原因:iOS11导航栏titleView图层有变化。iOS 10及以下 自定义titleView会添加在navigationBar上,iOS 11 添加在UINavigationBarContentView上。
解决办法:在设置自定义标题的时候,iOS11之后要使用自动布局设置位置和大小。直接贴代码:
func createTitleUI() { let titleBkView = UIView(frame: makeRect(x: 0, y: 0, width: 130, height: 44)) titleBkView.backgroundColor = WHITECOLOR titleBkView.isUserInteractionEnabled = true let tapGR = UITapGestureRecognizer(target: self, action: #selector(selectTypeAction)) titleBkView.addGestureRecognizer(tapGR) titleBkView.addSubview(titleLabel) titleBkView.addSubview(titleArrowImageView) titleLabel.snp.makeConstraints { (make) in make.center.equalToSuperview() } titleArrowImageView.snp.makeConstraints { (make) in make.centerY.equalToSuperview() make.left.equalTo(titleLabel.snp.right).offset(10) } self.navigationItem.titleView = titleBkView //注意:iOS11之后这里需要使用自动布局设置大小,否则位置会有偏差,无法响应点击事件 if #available(iOS 11.0, *) { titleBkView.snp.makeConstraints { (make) in make.size.equalTo(makeSize(width: 130, height: 44)) } } }
效果图如下:
产生问题的原因:iOS11导航栏titleView图层有变化。iOS 10及以下 自定义titleView会添加在navigationBar上,iOS 11 添加在UINavigationBarContentView上。
解决办法:在设置自定义标题的时候,iOS11之后要使用自动布局设置位置和大小。直接贴代码:
func createTitleUI() { let titleBkView = UIView(frame: makeRect(x: 0, y: 0, width: 130, height: 44)) titleBkView.backgroundColor = WHITECOLOR titleBkView.isUserInteractionEnabled = true let tapGR = UITapGestureRecognizer(target: self, action: #selector(selectTypeAction)) titleBkView.addGestureRecognizer(tapGR) titleBkView.addSubview(titleLabel) titleBkView.addSubview(titleArrowImageView) titleLabel.snp.makeConstraints { (make) in make.center.equalToSuperview() } titleArrowImageView.snp.makeConstraints { (make) in make.centerY.equalToSuperview() make.left.equalTo(titleLabel.snp.right).offset(10) } self.navigationItem.titleView = titleBkView //注意:iOS11之后这里需要使用自动布局设置大小,否则位置会有偏差,无法响应点击事件 if #available(iOS 11.0, *) { titleBkView.snp.makeConstraints { (make) in make.size.equalTo(makeSize(width: 130, height: 44)) } } }
效果图如下:
相关文章推荐
- 页面加UIPanGestureRecognizer手势后,无法响应tableViewCell点击事件的解决办法
- 页面加UIPanGestureRecognizer手势后,无法响应tableViewCell点击事件的解决办法
- 响应Android按钮点击事件并且弹出多个对话框的解决办法
- iOS开发之解决隐藏tabbar后原位置无法响应点击事件的问题
- 进行animation时点击事件无法响应的解决办法(animation进行时默认是禁止交互)
- DUI 响应选择行事件后,在本行再次点击无法响应选择行事件的解决办法
- iOS巅峰之解决隐藏tabbar后原位置无法响应点击事件的问题
- pointInside 和 hitTest (超出父视图点击事件不响应解决办法)
- 微信小程序中点击View中任何位置都可以触发相应事件的解决办法
- D7+Ehlib 7.0.133 DBGridEh,点击列标题排序属性设置好之后,有时无法排序的解决办法
- android 使用动画 Button移动后不响应点击事件的解决办法
- andorid布局layout监听点击事件,布局内的imageButton无法响应解决方法
- IE浏览器无法启动或者点击无响应的解决办法
- 关于子视图超出父视图后无法响应点击事件的处理办法
- ionic开发——百度地图InfoWindow信息窗口上添加按钮,无法触发点击事件问题解决办法
- UIScrollView无法响应touch事件的解决办法
- 『IOS』UIScrollView无法响应touch事件的解决办法
- GridView OnItemClickListener无法响应点击事件的解决
- UIScrollView无法响应touch事件的解决办法
- jQuery on()方法绑定动态元素的点击事件无响应的解决办法