简体   繁体   English

设置所有标签,文本字段和按钮(…layer.cornerRadius = 10)

[英]Set all labels, textfields and buttons (…layer.cornerRadius = 10)

how I can set ALL labels, textfields and buttons on my ViewController to ...layer.cornerRadius = 10? 如何在ViewController上将所有标签,文本字段和按钮设置为... layer.cornerRadius = 10? eg with a for-statement? 例如带有陈述? or must all be set individually? 还是必须全部单独设置?

You can put them all in an IBOutletCollection , which is an array generated through Interface Builder. 您可以将它们全部放入IBOutletCollection ,它是通过Interface Builder生成的数组。 Similar to how you create an outlet, select Outlet Collection in the popup that comes up. 与创建插座类似,在出现的弹出窗口中选择“插座集合”。 Then drag all of the other UI items you want to iterate over in there. 然后将要迭代的所有其他UI项拖到那里。 Note, though, that all of the items in a collections must be the same type. 但是请注意,集合中的所有项目都必须是同一类型。 So if you have labels and buttons, you'll need a separate collection for each. 因此,如果您有标签和按钮,则每个标签和按钮都需要一个单独的集合。

Alternatively, you can create a separate subclass of each type with their corner radius set (since you're apparently using that style of control so often) and use those classes in Interface Builder. 另外,您可以创建每种类型的单独子类,并设置其角半径(因为您显然经常使用这种控件样式),然后在Interface Builder中使用这些类。

1) Subclass the classes and add a UIAppearance corner radius property. 1)子类化并添加UIAppearance角半径属性。 This will give you reusable classes which you can use in different view controllers with different corner radius values. 这将为您提供可重用的类,您可以在具有不同角半径值的不同视图控制器中使用它们。 You'll have to create 3 new classes and remember to set all of them in Interface Builder when adding new items. 您将必须创建3个新类,并记得在添加新项目时在Interface Builder设置所有这些类。

2) Assuming everything is contained in the same view you can iterate through all of the subviews and check the class. 2)假定所有内容都包含在同一视图中,则可以遍历所有子视图并检查类。 If you end up using nested views you will need to check those subviews as well. 如果最终使用嵌套视图,则还需要检查那些子视图。 Not the cleanest but if it's a simple view it's a good option. 不是最干净的,但是如果它是一个简单的视图,这是一个不错的选择。

for (UIView *view in self.view.subviews)
{
    if ([view isKindOfClass:[UILabel class]] || [view isKindOfClass:[UIButton class]] || [view isKindOfClass:[UITextField class]])
    {
        view.layer.cornerRadius = 10.f;
    }
}

3) You can create an IBOutletCollection and loop through it in the same way without having to check the class. 3)您可以创建IBOutletCollection并以相同的方式遍历它,而无需检查类。 This becomes annoying as you add/remove items over time. 随着时间的推移添加/删除项目,这变得很烦人。

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

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