简体   繁体   中英

Storyboard Designtime Layout Attributes

This is a feature a like a lot when defining layouts for Android and that allows to define attributes just to design.

http://tools.android.com/tips/layout-designtime-attributes

But I didn't find any equivalent way to do the same when using storyboard to make iOS Apps.

At the moment I a cleaning all the design values on a viewDidLoad of my ViewController. Is there a way to define layout attributes as design placeholders and avoid making this setupClean step in all my view controllers?

When You want Instant Reflect of attributes/properties of particular componet in storyboard then you can achieve using @IBDesignable .

To achieve this You have to subcalss of that type of which you want to be reflected on storyboard.

Steps

  1. Make a subclass of the type you want be reflected on storyboard, as in example here i am going to subclass UILabel as DGlabel

  2. Make that calss as @IBDesignable , see the example

  3. Assign that subclassed to the component from Identity Inspector , see screen shot 在此处输入图片说明
  4. In subclassed calss (here in example DGLabel ) declare variables of properties/attributes which you want be reflected on design as @IBInspectable , see example here i have decalred borderColor as @IBInspectable , this means this property will be listed on property inspector of Xcode

now you can change the value of that property from story board as screen shot below

在此处输入图片说明

import UIKit

@IBDesignable
class DGLabel: UILabel {

@IBInspectable var borderColor:UIColor = UIColor.red {
    didSet {
        reflectChange()
    }
}

 override init(frame: CGRect) {
    super.init(frame: frame)
}

 required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}

func reflectChange() {

    self.layer.borderWidth = 1
    self.layer.borderColor = borderColor.cgColor
        }

}

Hope this will allow you to understand the process.

Yes you can do that by providing runtime attribute in "User defined runtime attributes" section under storyboard's Identity inspector. See below screenshot for setting UILabel's "text" keypath's runtime value as blank.

在此处输入图片说明

I think you can achieve something similar by using IBDesignable for your views. Then you may use prepareForInterfaceBuilder() and TARGET_INTERFACE_BUILDER to generate mock data for display in Interface Builder.

Did you mean this?

在此处输入图片说明

You can change Label Text value in storyboard from the "Attribute Inspector".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM