微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift:在 UIView 中添加的按钮不可点击

如何解决Swift:在 UIView 中添加的按钮不可点击

我有以下容器视图:

class NotificationsContainer: UIView {
        
    init() {
        super.init(frame: .zero)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        addSubview(controller.view)
        controller.view.isHidden = true
        self.isUserInteractionEnabled = true
        self.clipsToBounds = false
        
        configureAutoLayout()
    }
    
    var showNotifications = false {
        didSet {
            if showNotifications == true {
                controller.view.isHidden = false
            } else {
                controller.view.isHidden = true
            }
        }
    }
    
    internal lazy var notificationBanner: AlertView = {
        let banner = AlertView()
        banner.attrString = Uploadnotificationmanager.shared.notificationBannerText()
        banner.alertType = .notification
        banner.translatesAutoresizingMaskIntoConstraints = false
        addSubview(banner)
        banner.isUserInteractionEnabled = true
        banner.showMeButton.addTarget(self,action: #selector(showHideNotifications),for: .touchDown)
        return banner
    }()
    
    @objc func showHideNotifications() {
        showNotifications = showNotifications == false ? true : false
    }
    
    private lazy var notificationView: NotificationContentView = {
        let notificationView = NotificationContentView()
        return notificationView
    }()
    
    private lazy var controller: UIHostingController = {
        return UIHostingController(rootView: notificationView)
    }()
    
    private func configureAutoLayout() {
        NSLayoutConstraint.activate([
            notificationBanner.leadingAnchor.constraint(equalTo: leadingAnchor),notificationBanner.trailingAnchor.constraint(equalTo: trailingAnchor),controller.view.trailingAnchor.constraint(equalTo: notificationBanner.trailingAnchor),controller.view.topAnchor.constraint(equalTo: notificationBanner.bottomAnchor)
        ])
    }
}

AlertView 包含一个按钮,如下所示:

internal lazy var showMeButton: UIButton = {
  let button = UIButton()
    button.setTitle("Show me...",for: .normal)
    button.setTitleColor(UIColor.i6.blue,for: .normal)
    button.titleLabel?.font = .systemFont(ofSize: Constants.fontSize)
    addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

然后我将容器视图添加到我的主视图中:

private lazy var notifications: NotificationsContainer = {
    let notifications = NotificationsContainer()
    notifications.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(notifications)
    notifications.leadingAnchor.constraint(equalTo: flightNumber.leadingAnchor).isActive = true
    notifications.trailingAnchor.constraint(equalTo: flightNumber.trailingAnchor).isActive = true
    return notifications
}()

override public func viewDidLoad() {
    super.viewDidLoad()
    stackView.insert(arrangedSubview: notifications,atIndex: 0)
}

现在如您所见,我正在尝试向 showMeButton 添加一个操作。但是,当我单击按钮时,它什么也不做。我之前读过这可能与容器视图的框架有关。但是,我尝试在主视图中设置通知视图的高度(由于前导和尾随约束,宽度应该已经存在),并且我也尝试设置 notificationBanner 的高度,但没有任何效果

这是视图调试器中的视图:

enter image description here

showMe 按钮似乎没有被遮挡,所有其他视图似乎都有尺寸...

解决方法

查看 Xcode 中的调试视图层次结构,看看包含按钮的视图是否真正显示出来。您没有对这些视图中的任何一个设置足够的约束,因此高度和宽度看起来对我来说可能不明确。进入视图调试器后,另一个常见问题是另一个不可见的视图正在用按钮覆盖该视图并拦截触摸手势。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。