程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了添加 UITabbar 时 UIButton 不可点击大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决添加 UITabbar 时 UIButton 不可点击?

开发过程中遇到添加 UITabbar 时 UIButton 不可点击的问题如何解决?下面主要结合日常开发的经验,给出你关于添加 UITabbar 时 UIButton 不可点击的解决方法建议,希望对你解决添加 UITabbar 时 UIButton 不可点击有所启发或帮助;

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

添加 UITabbar 时 UIButton 不可点击

解决方法

@H_197_17@

我怀疑您尝试做的事情是不可能的,或者至少,Apple 不支持。 (因此不推荐,因为您可能会找到一种方法使其在今天可以运行,但在未来的某个操作系统版本中不能运行。)

通常,Apple 不支持您将自定义视图对象添加到标签栏、导航栏、堆栈视图、表/集合视图控制器等系统组件中,除非通过文档化的 API。

我建议不要做你想做的事情。相反,在标签栏控制器的内容视图中添加一个按钮。我不知道你是否能够像你试图做的那样让它部分覆盖标签栏。

,

您需要像这样覆盖自定义标签栏类中的 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
}

基本上问题是上部不可点击,因为它超出了标签栏的主要内容视图的范围。

此方法将检查点击是否在视图的边界内,如果是,它将返回视图并调用该按钮的操作。

苹果的文档:Link

P.s 我最近遇到了同样的问题,并得到了这个工作顺利的帮助。

大佬总结

以上是大佬教程为你收集整理的添加 UITabbar 时 UIButton 不可点击全部内容,希望文章能够帮你解决添加 UITabbar 时 UIButton 不可点击所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。