简体   繁体   English

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

[英]ios7 auto layout button resize issue

I am using xcode 5 with storyboards and autolayout turned ON. 我正在将xcode 5与情节提要和自动布局一起使用。 My layouts of one of my ViewControllers has a UIButton with an image background. 我的一个ViewController布局中有一个带有图像背景的UIButton。 The view controller also has 3 buttons anchored to the bottom of the view and some labels above. 视图控制器还具有固定在视图底部的3个按钮和上方的一些标签。 I laid out my storyboards to a 4" retina display (ie: iphone 5+) and the issue I have is when simulating to a 3.5" display (ie: iphone 4, 4s, etc) 我将情节提要板布置到4英寸的视网膜显示屏(即iphone 5+)上,而我遇到的问题是模拟到3.5英寸的显示屏(即iphone 4、4s等)时

I have pinned the height of the anchored buttons at the bottom to remain constant. 我将固定按钮的高度固定在底部,以保持恒定。 What I want is when the app runs on a 3.5" display iphone, that the UIButton with the image will resize smaller (keeping all other labels & buttons same size & spacing). So the aspect ratio would remain the same for that UIButton, it would just get smaller. 我想要的是当应用程序在3.5英寸显示屏iPhone上运行时,带有图像的UIButton的尺寸将减小(保持所有其他标签和按钮的尺寸和间距相同)。因此,该UIButton的长宽比将保持不变,会变小。

I haven't been able to find anything in the auto layout tutorial or others online about how to set up constraints to do this (where the height/width remain proportional). 在自动布局教程或其他在线指南中,我找不到任何有关如何设置约束以实现此目的的信息(高度/宽度保持成比例)。

I think what you really want is as follow: 1.Label's top is 100 from its superView and its bottom is 68 from its superView 2.In 4" display, its size is 200x400 with ratio .5 3.In 3.5" display, its size is 312x624 with ratio .5 我认为您真正想要的是: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];
}

Checkout the 'Taking Control of Auto Layout in Xcode 5' video from the WWDC 2013 Videos -- Quickly: the top part of the Add New Constraints area is probably what your looking for WWDC 2013视频中快速检出“控制Xcode 5中的自动布局”视频-快速:“添加新约束”区域的顶部可能是您想要的内容

添加新约束

I guess preserving aspect ratio is NOT possible in auto layouts via interface builder. 我猜想通过界面生成器在自动布局中无法保留宽高比。 #fail. #失败。

According to this: http://www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-2 , at the very end of the tutorial. 据此: http : //www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-2 ,位于本教程的最后。

So, i guess I need to do this programmatically somehow. 因此,我想我需要以编程方式以某种方式执行此操作。

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

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