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
113 views
in Technique[技术] by (71.8m points)

Swift: button added within UIView not clickable

I have the following container view:

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 contains a button as follows:

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

Then I add the container view to my main view:

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

Now as you can see I am trying to add an action to the showMeButton. However, when I click on the button, it does nothing. I have read before that this could be to do with the frame of the container view. However, I have tried setting the height of the notification view in my main view (width should already be there due to leading and trailing constraints) and I have tried setting the height of notificationBanner as well but nothing is working.

Here is the view in the view debugger:

enter image description here

The showMe button does not appear to be obscured and all other views appear to have dimensions...

question from:https://stackoverflow.com/questions/65893217/swift-button-added-within-uiview-not-clickable

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

1 Answer

0 votes
by (71.8m points)

Look at the debug view hierarchy in Xcode and see if the view containing the button is actually showing up. You haven't set enough constraints on any of these views so the height and width look like they could be ambiguous to me. Once you're inside the view debugger, another common problem is that another invisible view is covering up the one with the button and intercepting the touch gestures.


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

...