gontovnik / DGRunkeeperSwitch

Runkeeper design switch control
MIT License
1.88k stars 175 forks source link

Border radius not working when using autolayout in code #16

Closed king7532 closed 8 years ago

king7532 commented 8 years ago

In the example app, modify the ViewController.swift's runkeeperSwitch2 to be laid out using autolayout programmatically, like:

        let runkeeperSwitch2 = DGRunkeeperSwitch()
        runkeeperSwitch2.leftTitle = "Weekly"
        runkeeperSwitch2.rightTitle = "Monthly"
        runkeeperSwitch2.backgroundColor = UIColor(red: 239.0/255.0, green: 95.0/255.0, blue: 49.0/255.0, alpha: 1.0)
        runkeeperSwitch2.selectedBackgroundColor = .whiteColor()
        runkeeperSwitch2.titleColor = .whiteColor()
        runkeeperSwitch2.selectedTitleColor = UIColor(red: 239.0/255.0, green: 95.0/255.0, blue: 49.0/255.0, alpha: 1.0)
        runkeeperSwitch2.titleFont = UIFont(name: "HelveticaNeue-Medium", size: 13.0)
        //runkeeperSwitch2.frame = CGRect(x: 50.0, y: 20.0, width: view.bounds.width - 100.0, height: 30.0)
        //runkeeperSwitch2.autoresizingMask = [.FlexibleWidth]
        runkeeperSwitch2.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(runkeeperSwitch2)

        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[switch]-20-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["switch":runkeeperSwitch2]))
        view.addConstraint(NSLayoutConstraint(item: runkeeperSwitch2, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 10))
        runkeeperSwitch2.addConstraint(NSLayoutConstraint(item: runkeeperSwitch2, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 30.0))

And this is what happens in the iOS simulator:

simulator screen shot dec 9 2015 12 27 03 am

Do you have any idea why the border radius is not working using the above code?

king7532 commented 8 years ago

I found the problem, DGRunkeeperSwitchRoundedLayer only sets the cornerRadius on it's layer when the frame property is set. However when using autolayout constraints on iOS, the frame property is never set and therefore the cornerRadius defaults to 0. iOS autolayout constraints set the view's center and bounds properties instead of the frame.

To make this work, I changed DGRunkeeperSwitchRoundedLayer:

public class DGRunkeeperSwitchRoundedLayer: CALayer {

    override public var cornerRadius: CGFloat {
        get { return CGFloat(bounds.height / 2.0) }
        set { super.cornerRadius = newValue }
    }   
}
king7532 commented 8 years ago

See PR #18

gontovnik commented 8 years ago

Please check the latest commit and let me know if it works

king7532 commented 8 years ago

Yes the latest master works for me. Thanks

king7532 commented 8 years ago

Just curious why you decided to go with overriding bounds instead of cornerRadius?

gontovnik commented 8 years ago

I think it is just cleaner to update cornerRadius on bounds change instead of overriding cornerRadius getter