CALayer--to be continued

CALayer sits beneath all your UIViews (that's the parent of UIButton, UILabel, and so on), so it's like an exposed underbelly giving you lots of options for modifying the appearance of views, as long as you don't mind dealing with a little more complexity.

为了给button添加边界,使用calayer的borderWidth:

button.layer.borderWidth = 1

it draws a one point black line around the button. the line is black by default, but you can change the colour by using UIColor data type. CALayer sits at a lower technical level than UIButton, which means it doesn't understand what a UIColor is. UIButton knows what a UIColor is because they are both at the same technical level, but CALayer is below UIButton, so UIColor is a mystery.

Don't despair, though: CALayer has its own way of setting colors called CGColor, which comes from Apple's Core Graphics framework. This, like CALayer, is at a lower level than UIButton, so the two can talk happily – again, as long as you're happy with the extra complexity. Even better, UIColor (which sits above CGColor) is able to convert to and from CGColor easily, which means you don't need to worry about the complexity – hurray!

button.layer.borderColor = UIColor.lightGrayColor().CGColor

你可能感兴趣的:(CALayer--to be continued)