Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

swift - UIButton Not Clickable when added UITabbar

When added UIButton on UITabbar to middle as shown in figure. The button action on above the UITabBar unable to click

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)
    
}

enter image description here

question from:https://stackoverflow.com/questions/65942546/uibutton-not-clickable-when-added-uitabbar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suspect that what you are trying to do is not possible, or at the least, not supported by Apple. (And thus not recommended since you might find a way to make it might work today but not in some future OS version.)

As a rule, Apple does not support you adding custom view objects to system components like tab bars, navigation bars, stack views, table/collection view controllers, etc except through a documented API.

I would suggest NOT doing what you are trying to do. instead, add a button in the content view of the tab bar controller. I don't know if you'll be able to make it partly cover the tab bar like you are trying to do however.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...