我尝试在 NavigationItem 上添加按钮和 ImageView。对于这个对象,我为它添加了两个函数 TouchUp。这两个对象是我在 uiview 上添加的。我添加到 UINavigaionItem 的 UIView。我为 ImageView 添加了 UIGesture,为按钮添加了 TouchUp 操作。但是按钮和 ImageView 没有点击。 代码如下:
let navView = UIView()
navView.userInteractionEnabled = true
let btn = UIButton(type: UIButtonType.Custom) as UIButton
btn.addTarget(self, action: #selector(ViewController.on_clicked_btn(_:)), forControlEvents: UIControlEvents.TouchUpInside)
btn.frame = CGRectMake(-65, -22, 130, 42)
btn.setTitle("title", forState: .Normal)
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
btn.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
btn.titleLabel?.backgroundColor = UIColor.clearColor()
photo.frame = CGRect(x: -110, y: -18, width: 33, height: 33)
photo.layer.borderWidth = 0.5
photo.layer.masksToBounds = false
photo.layer.borderColor = UIColor.grayColor().CGColor
photo.layer.cornerRadius = self.photo_avatar.frame.height/2
photo.clipsToBounds = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.on_clicked_photo(_:)))
photo.addGestureRecognizer(tapRecognizer)
photo.userInteractionEnabled = true
navView.addSubview(photo)
navView.addSubview(btn)
self.navigationItem.titleView = navView
有什么想法吗?感谢您的帮助。
最佳答案
这是因为你没有初始化 UIIview rect in:
let navView = UIView()
将其替换为
let navView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 168, height: 40))
将 bin 框架替换为:
btn.frame = CGRect(x: 38, y: 0, width: 130, height: 42)
和相框
photo.frame = CGRect(x: 0, y: 0, width: 33, height: 33)
关于ios - 向 NavigationItem 添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47865820/