KkM*_*MIW 2 uibutton uitabbar swift
当将UITabbar上的UIButton添加到中间时如图所示。UITabBar上面的按钮动作无法点击
func setupMiddleButton() {
plusButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
var menuButtonFrame = plusButton.frame
menuButtonFrame.origin.x = tabBar.bounds.width/2 - menuButtonFrame.size.width/2
let hasNotched :Bool? = UIDevice.current.hasNotch
if hasNotched != nil {
menuButtonFrame.origin.y = tabBar.bounds.height - menuButtonFrame.height - 15
} else {
menuButtonFrame.origin.y = tabBar.bounds.height - menuButtonFrame.height - 50
}
plusButton.frame = menuButtonFrame
plusButton.setTitle("+", for: .normal)
plusButton.titleLabel?.font = UIFont.helveticaNeue(ofSize: 40)
plusButton.backgroundColor = UIColor.init(hexString: "5E71FE")
plusButton.titleEdgeInsets = UIEdgeInsets(top: 0,left: 10,bottom: 10,right: 10)
tabBar.addSubview(plusButton)
plusButton.layer.cornerRadius = menuButtonFrame.height/2
plusButton.addTarget(self, action: #selector(plusButtonAction(sender:)), for: .touchUpInside)
}
Run Code Online (Sandbox Code Playgroud)
您需要像这样重写自定义选项卡栏类中的 hitTest 方法
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
{
guard !clipsToBounds && !isHidden && alpha > 0 else { return nil }
for member in subviews.reversed()
{
let subPoint = member.convert(point, from: self)
guard let result = member.hitTest(subPoint, with: event)
else { continue }
return result
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
基本上问题是上部不可点击,因为它超出了选项卡栏主要内容视图的范围。
此方法将检查点击是否在视图的边界内,如果是,它将返回视图并调用该按钮的操作。
苹果的文档:链接
Ps 我最近遇到了同样的问题,并得到了这个帮助,效果很顺利。
| 归档时间: |
|
| 查看次数: |
1047 次 |
| 最近记录: |