ios - 使用 NSAutolayout 概念如何在 ios 中的同一位置显示所有版本的按钮

标签 ios iphone objective-c

您好,我正在以编程方式创建按钮。我正在 ios6 和 ios7 4 英寸屏幕和 3.5 英寸屏幕上运行一些应用程序。我需要在同一位置显示所有版本的按钮。

所以我想为此使用自动布局概念。我知道如何以编程方式创建按钮,但我不知道如何为按钮应用自动布局概念。这是我的代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button addTarget:self action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@"Show View" forState:UIControlStateNormal];

button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

[view addSubview:button];

最佳答案

试试这个,

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"Show View" forState:UIControlStateNormal];

    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

    [self.view addConstraints:@[

                                //view1 constraints
                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeTop
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeTop
                                                            multiplier:1.0
                                                              constant:padding.top],

                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeLeft
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeLeft
                                                            multiplier:1.0
                                                              constant:padding.left],

                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeBottom
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeBottom
                                                            multiplier:1.0
                                                              constant:-padding.bottom],

                                [NSLayoutConstraint constraintWithItem:button
                                                             attribute:NSLayoutAttributeRight
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.view
                                                             attribute:NSLayoutAttributeRight
                                                            multiplier:1
                                                              constant:-padding.right],

                                ]];

    [self.view addSubview:button];

更新:

button.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin);

引用链接:Autoresizing masks programmatically vs Interface Builder / xib / nib

关于ios - 使用 NSAutolayout 概念如何在 ios 中的同一位置显示所有版本的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23131886/

相关文章:

iphone - 使用 UIScrollView 隐藏键盘,不会出现任何故障

iOS SWIFT - NSJSONSerialization 后的 JSON 对象为 nil

ios - 如何以编程方式禁用 UITextField 中的复制粘贴选项

ios - 在 iOS 中的核心数据和 NSFetchedResultsController 中点击我的屏幕之前,reloadData 无法正常工作

iphone - 如何将 NSRegularExpression 写入 xx :xx:xx?

iphone - 您如何循环播放iPhone的声音?

objective-c - 请在 Cocoa 示例中录制声音!

ios - Xcode Storyboard - 使用很棒的字体制作文本 RTL

ios - 用于在多个 ViewController 上显示标签的自定义 UIView?

ios - 使用 iBeacon 时禁用 "Location Accuracy"消息