簡體   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