ios - 自动布局 - 以编程方式垂直排列元素并在元素之间留有空间?

标签 ios user-interface uilabel frame autolayout

我目前正在手动定位我的 UI 元素,如果我更新文本或添加元素,这会很烦人,因为我每次都必须重新计算所有位置。

有没有办法使用自动布局来调整元素的大小和位置,例如如下所示,无论标签包含多少文本?

UILabel (multiline, variable height)
[20px gap]
UILabel (multiline, variable height)
[20px gap]
UIButton

最佳答案

是的,它看起来像这样

@implementation MyClass {
    NSDictionary *_viewsDictionary;
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // First create your controls - you can just use CGRectZero
        _label1 = [[UILabel alloc] initWithFrame:CGRectZero];
        _label1 setText:@"Some text";

        _label2 = [[UILabel alloc] initWithFrame:CGRectZero];
        _label2 setText:@"Some text 2";

        _button = [[UIButton alloc] initWithFrame:CGRectZero];

        // Then just add them to your as a sub view
        [self addSubview:self.currentBalanceLabel];
        [self addSubview:_nameLabel];
        [self addSubview:_button];

        // Put them in an NSDictionary - this is a macro and will be used when setting up the contraints below
        _viewsDictionary = NSDictionaryOfVariableBindings(_nameLabel, _currentBalanceLabel,_button);

        // This tells the view to run update contraints
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
    }
}


- (void)updateConstraints
{
    [super updateConstraints];

    // Using the _viewsDictionary, we must tell always tell how all the controls will be setup both 
    // horizontally and vertically.  In this case wear are going to tell the label to take the entire width
    // The rest of the vies will be aligned on the left below
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_label1]-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:_viewsDictionary];

    //Add the contraints
    [self addConstraints:constraints];

    // Next setup the vertical contraints.  This is what you asked about spefically, label - 20 - label - 20 - button
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_labelq]-20-[_label2]-20-[_button]"
                                                          options: NSLayoutFormatAlignAllLeft
                                                          metrics:nil
                                                            views:_viewsDictionary];


    [self addConstraints:constraints];


}

@end

关于ios - 自动布局 - 以编程方式垂直排列元素并在元素之间留有空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21176878/

相关文章:

ios - 如何在同一个 UILabel 中添加多行

ios - 以编程方式从 iPhone 上的收件箱中读取电子邮件

ios - 如何在 UILabel 中显示表达程度? ( swift )

iphone - 如何向 IBOutletCollection 添加对象?

objective-c - MKMapView 的暗淡部分

ios - didUpdateLocations 在 Swift 3 中不起作用

android - 使用后端创建移动聊天应用程序的多种方法

c++ - 如何使用 Direct2D 创建自定义窗口镶边?

c - 如何构建我想要的 GTK GUI 应用程序?

java - 如何从多维数组的listview中获取值?