简体   繁体   English

情节提要Designtime布局属性

[英]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. 在为Android定义布局时,此功能非常相似,并且可以定义仅用于设计的属性。

http://tools.android.com/tips/layout-designtime-attributes 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. 但是在使用情节提要制作iOS应用程序时,我没有找到任何等效的方法来做同样的事情。

At the moment I a cleaning all the design values on a viewDidLoad of my ViewController. 目前,我正在清理ViewController的viewDidLoad上的所有设计值。 Is there a way to define layout attributes as design placeholders and avoid making this setupClean step in all my view controllers? 有没有一种方法可以将布局属性定义为设计占位符,并避免在所有视图控制器中执行setupClean步骤?

When You want Instant Reflect of attributes/properties of particular componet in storyboard then you can achieve using @IBDesignable . 如果要在情节提要中即时反映特定组件的属性/属性,则可以使用@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 制作想要反映在情节提要上的类型的子类,例如在这里的示例中,我将UILabel子类化为DGlabel

  2. Make that calss as @IBDesignable , see the example 将其设置为@IBDesignable ,请参见示例

  3. Assign that subclassed to the component from Identity Inspector , see screen shot Identity Inspector将该子类分配给组件,请参见屏幕快照 在此处输入图片说明
  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 在子类化的cals中(此处为DGLabel示例),将要在设计中反映出来的属性/属性变量声明为@IBInspectable ,请参见此处的示例,我将borderColor的 标签@IBInspectable ,这意味着此属性将在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. 请参见下面的屏幕快照,以将UILabel的“文本”键路径的运行时值设置为空白。

在此处输入图片说明

I think you can achieve something similar by using IBDesignable for your views. 我认为您可以通过将IBDesignable用于视图来实现类似的IBDesignable Then you may use prepareForInterfaceBuilder() and TARGET_INTERFACE_BUILDER to generate mock data for display in Interface Builder. 然后,您可以使用prepareForInterfaceBuilder()TARGET_INTERFACE_BUILDER生成模拟数据以在Interface Builder中显示。

Did you mean this? 你是这个意思吗

在此处输入图片说明

You can change Label Text value in storyboard from the "Attribute Inspector". 您可以从“属性检查器”中的情节提要中更改“标签文本”值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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