【问题标题】:Swift: button added within UIView not clickableSwift:在 UIView 中添加的按钮不可点击
【发布时间】:2021-01-25 22:20:07
【问题描述】:

我有以下容器视图:

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 的高度,但没有任何效果。

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

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

【问题讨论】:

    标签: swift uiview uibutton constraints


    【解决方案1】:

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

    【讨论】:

    • 谢谢 - 我看了看,所有元素似乎都有宽度和高度,而视图没有被任何东西挡住......
    • 嘿在这里banner.showMeButton.addTarget(self, action: #selector(showHideNotifications), for: .touchDown) 将方法更改为.touchUpInside
    • 其实我现在想通了。问题是我没有为notificationBanner 设置一个顶级锚。一旦我把它固定到容器视图的顶部,然后设置一个高度,它就完美了!
    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2022-11-12
    • 2011-08-16
    相关资源
    最近更新 更多