繁体   English   中英

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

[英]ios7 auto layout button resize issue

我正在将xcode 5与情节提要和自动布局一起使用。 我的一个ViewController布局中有一个带有图像背景的UIButton。 视图控制器还具有固定在视图底部的3个按钮和上方的一些标签。 我将情节提要板布置到4英寸的视网膜显示屏(即iphone 5+)上,而我遇到的问题是模拟到3.5英寸的显示屏(即iphone 4、4s等)时

我将固定按钮的高度固定在底部,以保持恒定。 我想要的是当应用程序在3.5英寸显示屏iPhone上运行时,带有图像的UIButton的尺寸将减小(保持所有其他标签和按钮的尺寸和间距相同)。因此,该UIButton的长宽比将保持不变,会变小。

在自动布局教程或其他在线指南中,我找不到任何有关如何设置约束以实现此目的的信息(高度/宽度保持成比例)。

我认为您真正想要的是:1.Label的superView顶部是100,superView的底部是68。4。“ 4”显示器的尺寸是200x400,比率为.5 3.3.5“” 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];
}

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

添加新约束

我猜想通过界面生成器在自动布局中无法保留宽高比。 #失败。

据此: http : //www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-2 ,位于本教程的最后。

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

暂无
暂无

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

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