ios7自动布局按钮调整大小问题

时间:2013-11-06 04:21:30

标签: ios iphone button layout

我正在使用xcode 5,故事板和自动布局打开。我的一个ViewControllers的布局有一个带有图像背景的UIButton。视图控制器还有3个按钮锚定在视图的底部,上面有一些标签。我把我的故事板放到一个4英寸的视网膜显示器上(即:iphone 5+),我的问题是模拟到3.5英寸显示器(即:iphone 4,4s等)

我已将固定按钮的高度固定在底部以保持不变。我想要的是当应用程序在3.5英寸显示器iphone上运行时,带有图像的UIButton将调整得更小(保持所有其他标签和按钮的大小和间距相同)。因此,UIButton的宽高比将保持不变,它会变小。

我无法在汽车布局教程或其他网上找到有关如何设置约束来执行此操作的任何内容(高度/宽度保持成比例)。

3 个答案:

答案 0 :(得分:1)

我认为你真正想要的是如下: 1.Label的顶部是超级视图100,底部是超级视图68 2.在4“显示器中,其尺寸为200x400,比例为.5 3.在3.5英寸显示屏中,其尺寸为312x624,比率为.5

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // create search bar
    CGRect frame = CGRectMake(60, 100, 200, 400);
    _label = [[UILabel alloc] initWithFrame:frame];
    _label.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_label];

    // layout search bar
    _label.translatesAutoresizingMaskIntoConstraints = NO;
    // height
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_label
                                                                  attribute:NSLayoutAttributeHeight
                                                                  relatedBy:NSLayoutRelationLessThanOrEqual
                                                                     toItem:nil
                                                                  attribute:NSLayoutAttributeNotAnAttribute
                                                                 multiplier:0
                                                                   constant:400];
    // width
    [_label addConstraint:constraint];
    constraint = [NSLayoutConstraint constraintWithItem:_label
                                              attribute:NSLayoutAttributeWidth
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:_label
                                              attribute:NSLayoutAttributeHeight
                                             multiplier:0.5
                                               constant:0];
    [_label addConstraint:constraint];

    // vertical
    NSDictionary *views = NSDictionaryOfVariableBindings(_label, self.view);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[_label]-68-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];
    [self.view addConstraints:constraints];

    // horizontal
    constraint = [NSLayoutConstraint constraintWithItem:_label
                                              attribute:NSLayoutAttributeCenterX
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                              attribute:NSLayoutAttributeCenterX
                                             multiplier:1
                                               constant:0];
    [self.view addConstraint:constraint];
}

答案 1 :(得分:0)

WWDC 2013 Videos查看“控制Xcode 5中的自动布局”视频 - 快速:“添加新约束”区域的顶部可能是您要查找的内容

Add New Constraints

答案 2 :(得分:0)

我认为通过界面构建​​器在自动布局中无法保留纵横比。 #fail。

根据这个:http://www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-2,在本教程的最后。

所以,我想我需要以某种方式以编程方式执行此操作。

相关问题