本文介绍了布局:如何使用视觉格式的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级.

父类具有一个属性:

@interface ParentVC

    @property (nonatomic, strong) UIImageView* logoImageView;
@end

因此,现在,我需要在子类中输出具有布局约束的视图.

So, now, I need to output this view with layout constraints in subclass.

@interface ChildVC : ParentVC
@end

如何更正视觉格式的格式字符串?

How to correct format string of visual format?

@"V:|-(%i)-[self.logoImageView]" // wrong

我找到了解决方法:

UIView* selfLogoImageView = self.logoImageView;

@"V:|-(%i)-[selfLogoImageView]"

但是有没有新变量的方法吗?

But is there any method without new variable?

推荐答案

由于您使用的是视觉格式语言,因此您同时也在使用[NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views].该方法需要views:参数的字典.您正在使用NSDictionaryOfVariableBindings吗?如果是这样,则不必.您可以传入任何旧字典,只要它将您的视图映射到您在视觉格式字符串中使用的名称即可.顺便说一句,您也可以传递指标字典,而不是使用%i格式说明符.

Since you are using the visual format language, you are also using [NSLayoutConstraint constraintsWithVisualFormat:options:metrics:views]. That method expects a dictionary for the views: parameter. Are you using NSDictionaryOfVariableBindings? If so, you don’t have to. You can pass in any old dictionary, as long as it maps your views to names that you use in the visual format string. Incidentally, you can also pass a metrics dictionary, instead of what you are doing with the %i format specifier.

NSDictionary *views = @{ @"logo" : self.logoImageView, // use any name you want here
                         @"label" : self.someLabelView };

// kSomeSpacerConstant is an int or float primitive constant.
// We are wrapping it in @() because this dictionary needs NSNumbers as values.
NSDictionary *metrics = @{ @"spacer" : @(kSomeSpacerConstant) };

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(spacer)-[logo]-[label]"
                                                                  options:0
                                                                  metrics:metrics
                                                                    views:views]];

如果您不想仅仅为了保存指标或视图而创建新的NSDictionary,只需为metrics:views:参数传递文字@{ ... }语法. (但是我建议您不要使用更简洁的代码,或者如果您要构建多个视觉格式的字符串,并且想要重用字典,则为之.)

If you don’t want to make a new NSDictionary just to hold your metrics or views, just pass in the literal @{ ... } syntax for the metrics: or views: parameters. (But I advise against it for the sake of cleaner code, or if you are building more than one visual format string and you want to reuse the dictionaries.)

这篇关于布局:如何使用视觉格式的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:18
查看更多