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

标签 ios iphone button layout

我使用的是 xcode 5, Storyboard和自动布局已打开。我的一个 ViewController 的布局有一个带有图像背景的 UIButton。 View Controller 还有 3 个按钮固定在 View 的底部和上面的一些标签。我将 Storyboard布置到 4"视网膜显示器(即:iphone 5+),我遇到的问题是模拟到 3.5"显示器(即:iphone 4、4s 等)

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

我无法在自动布局教程或其他在线教程中找到有关如何设置约束以执行此操作的任何内容(其中高度/宽度保持比例)。

最佳答案

我想你真正想要的是如下: 1.Label的顶部距其superView为100,底部距其superView为68 2.在 4"显示器中,其尺寸为 200x400,比例为 0.5 3.在3.5寸显示器中,尺寸为312x624,比例为0.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];
}

关于ios7自动布局按钮调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19804062/

相关文章:

iphone - FFMPEG 的非 GPL 替代品?

iPhone 应用程序运行时错误 - "Error: Embedded profile header length is greater than data length.\n"

ios - 如何为 MKMapKit View 编写代码当用户单击 pin 时,它会显示另一个容器 View ?

javascript - 快速播放声音

Python - Tkinter 按钮命令不起作用

ios - 将 PDF 保存到文档(PDF 已损坏)

ios - 在 Swift 的屏幕上的列中显示数组中的字符串?

ios - iOS 13 撤消/重做手势的手势识别器在哪里?

ios - 如何在不删除现有对象的情况下将对象添加到 NSMutableArray 中?

android - 在android中按下按钮时如何删除某些元素