Nick Guerra
Nick Guerra

Reputation: 21

UIButton doesn't work when the searchController is active?

I'm using a UICollectionViewCompositionalLayout with a search controller:

just like this

The Problem:

I can't tap on the UI Button present in the header of Section when the search controller is active. But the GestureRecognizer of the Section just works fine.

Both of them works when I'm not searching anything, and if a tap "enter" on keyboard while searching, the button start to work.

What I'm doing wrong?

Upvotes: 0

Views: 125

Answers (1)

Nick Guerra
Nick Guerra

Reputation: 21

I just found an solution. For those who are having problem: I was adding the Target this way

let button: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("See all", for: .normal)
        button.setImage(UIImage(systemName: "chevron.right", withConfiguration: buttonConfig), for: .normal)
        
        button.addTarget(self, action: #selector(myFunc), for: .touchUpInside)
        
        return button
    }()

Removing the addTarget, and adding it on the Init of the class fixes the Problem.

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(button.addTarget)
    button.addTarget(self, action: #selector(myFunc), for: .touchUpInside)
}

I Don't know why, but it works. lol

Upvotes: 2

Related Questions