Search code examples
iosswiftswift5sirishortcuts

Swift button in button


I would like to make a button like the Shortcuts app has.
A big button and inside an other small button.
The small button would open a new view and the big button just run a simple print statement for example.

If the solution is a pod i would be more than happy but i think its nothing difficult just im not paying attention :)

I was able to do that with this stuff:

func add(secondButton: UIButton, buttonName: UIButton, secondButtonTitle: String, secondIsEnabled: Bool) {
    secondButton.isEnabled = secondIsEnabled
    print(secondIsEnabled)
    if #available(iOS 13.0, *) {
        secondButton.backgroundColor = UIColor.systemFill
        secondButton.setTitle(secondButtonTitle, for: .normal)
        if buttonName.isEnabled {
            buttonName.backgroundColor = UIColor.systemRed
            buttonName.tintColor = .systemGray4
        } else {
            buttonName.backgroundColor = UIColor.systemOrange
            buttonName.tintColor = .systemGray2
        }
    }else {
        // Fallback on earlier versions
        // TODO: do
        if buttonName.isEnabled {

        } else {
            buttonName.backgroundColor = color
        }
    }
    buttonName.layer.cornerRadius = 4
    view.addSubview(buttonName)

    secondButton.tintColor = .black
    secondButton.frame = CGRect(x: 5.0, y: 5.0, width: 60.0, height: 15.0)
    secondButton.layer.cornerRadius = 4
    buttonName.addSubview(secondButton)
    buttonName.bringSubviewToFront(secondButton)
}

But if the big button is disabled, the small button will disable too

I want to keep the current design which is this The reason why the last button is red because it is enabled
sorry for the button titles its in Hungarian and i forgot to translate, they are just numbers from 1 to 6

My problem starts here:
There are situations where the big button is disabled. But I want to keep the smaller button enabled all the time, even if the big button is disabled.
Is this possible?

If im not mistaken my search word for google would be nested buttons but i couldn't really find anything


Solution

  • Problem:

     buttonName.addSubview(secondButton)
    

    This is causing your second button to be disabled when the big button is disabled. Instead, add both to the subview of your main view, then change the layer property so the small/second button appears inside your on top of your big button.

    Solution:

    buttonName.layer.zPosition = 0
    secondButton.layer.zPosition = 1
    view.addSubview(secondButton)