iPhone5 Storyboard自动布局

标签 iphone ios6 retina-display autolayout

我正在使用自动布局编写一个 iOS 应用程序。 我想在屏幕 Y 中心下方 30 点处放置一个按钮。 我如何为此添加约束?

最佳答案

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *superView = self.view;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTranslatesAutoresizingMaskIntoConstraints:NO];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    [superView addSubview:button];

    // adding constraints to the button
    NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:button
                             attribute:NSLayoutAttributeTop
                             relatedBy:NSLayoutRelationEqual
                                toItem:superView
                             attribute:NSLayoutAttributeCenterY
                            multiplier:1.0
                              constant:30];// adds 30 points from Screen Center Y
    [superView addConstraint:cn];


    cn = [NSLayoutConstraint constraintWithItem:button
                                      attribute:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:superView
                                      attribute:NSLayoutAttributeCenterX
                                     multiplier:1.0
                                       constant:0.0];// places the button in middle of X axis
    [superView addConstraint:cn];

}

关于iPhone5 Storyboard自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719918/

相关文章:

iphone-sdk : Adding a textfield to UIAlertview does not work in iOS 4?

iphone - 如何在 Xcode 中的 iPhone iOS 6 应用程序上获得从 A 到 B 的方向?

iphone - 模拟器、设备和应用商店的行为测试有什么区别

ios - 真实广告 (admob) 未显示在 Live 应用中

iphone - Xcode 4.5 : Undefined symbols for architecture armv7s:

iphone - iPhone 是否需要高分辨率和标准分辨率的副本?

iphone - TabBarApp 中的 Springs/Struts 不会显示整个 UIImages?

ios - 更改屏幕尺寸 ios 时 View 出现故障

iphone - 在应用程序中显示全黑屏幕

ios - 重复使用 UICollectionView 中的单元格时,会短暂显示重复使用的 UICollectionViewCell 的旧内容