Runtime Attribute is a feature that is not valued but is very powerful, and it can help you write more lightweight viewcontroller.
1, under what circumstances to use
When you use IB (Storyboard or xib) to edit a view, you may sometimes encounter attributes such as rounded corners, borders, border colors, and so on that are difficult to set directly through IB.
At this point you have to use code to implement. In fact, when this happens, we can actually use the runtime attribute in ib.
2. How to use
, in IB, click any of the controls to switch to the identity inspector
Figure 1
New properties in user Defined Runtime attributes
Here you can set the value for the control to
Boolean-bool
Number-nsnumber object or enumeration or Nsinteger or CGFloat
String-nsstring *
Point-cgpoint
Size-cgsize
Range-nsrange
Color-uicolor *
Properties, but must be called
For example Layer.cornerradius layer.maskstobounds here is actually the object. Layer.cornerradius Call Relationship
At the same time, you can even set a custom property for it
For example, you can arbitrarily expand a fame status property to indicate an open state, but only if you have to customize a reference for the control that implements the runtime auto-build member.
As shown in 1, here is a layer.bordercolorfromeuicolor, which is actually what I have in order to solve IB cannot set Cgcolor and a property introduced for Calayer.
As shown in 2, we extend a property for Calayer in Swift
Figure 2
Use it as a Layer.bordercolorfromuicolo (standard set method call), so IB can also set the Cgcolor
3. DEMO
Figure 3
As shown in the new IB file 3, the Swift project puts the following code in any file
Extension calayer{
func setbordercolorfromuicolor (color:uicolor) {
Self. bordercolor = color. Cgcolor
}
}
Compile and run, and implement the set fillet, border, border color for the control in IB
IOS uses Runtime Attribute to add properties that cannot be directly set for IB controls