freshOS / Stevia

:leaves: Concise Autolayout code
https://freshos.github.io/SteviaDocs/
MIT License
3.38k stars 211 forks source link

Styling when frame has not been set #23

Closed mgallagher closed 8 years ago

mgallagher commented 8 years ago

Forgive me if I'm using all the wrong terms here. I'm still quite new at this. Stevia's example project separates the View from the ViewController, and all subviews are layed out in the render() function in order for it to work nicely with the code injection plugin. As someone trying to move away from storyboards though, the issue I'm running into now is how to make a circular UIButton.

Normally I would do this with an IB or by setting the runtime attributes directly in the interface builder:

someButton.layer.cornerRadius = someButton.frame.height / 2.0
someButton.clipsToBounds = true

Trying to do this in code/Stevia though, by the time render() is called, the button's frame = 0.0, so setting a cornerRadius does nothing and the button is still square. How would you approach this? One thing I've found is to explicitly set the frame size with the UIButton(frame: CGRect) initializer, but something about setting the frame and then later in render() setting its height and width attributes to the same dimensions just doesn't sit well with me...

ghost commented 8 years ago

You could override layoutSubviews and place your code after super.layoutSubviews()

s4cha commented 8 years ago

Quick question here, is there a particular reason why you wouldn't use a classic (aka squared UIButton) with a round image?

button.setImage("myRoundImage", forState: .Normal)

In an ideal auto layout world, we want to avoid referencing frame as much as possible :)