Search code examples
iosswiftuikit

How to set first item selected by default when making a url request?


I am using uiKit framework. I am using stack view and inside that are buttons, and data of that buttons is coming from server. I want that when i receive data, first button in stack view will be selected by defalut and the color changes. can anyone help please?


Solution

  • You need to get the first view that is a UIButton and then set his selected property to true

    DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
            if let button = self?.stackview.arrangedSubviews.first(where: { $0 is UIButton }) as? UIButton {
                button.isSelected = true
        }
    }