IOS uses Runtime Attribute to add properties that cannot be directly set for IB controls

Source: Internet
Author: User
Tags border color

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.