将 UIButton 定位在屏幕顶部 [英] Positioning UIButtons at top of Screen

查看:39
本文介绍了将 UIButton 定位在屏幕顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是在屏幕顶部等距放置 5 个按钮.我尝试使用约束,但按钮会被压扁.

What I want is to have 5 buttons equally spaced out at the top of the screen. I tried using constraints but the buttons would get squished.

到目前为止我有这个代码:

I have this code so far:

- (void)viewDidLoad {
    [super viewDidLoad];
    //I know this is only two buttons
    NSUInteger space =  (_backround.frame.size.width - _squareOne.frame.size.width  * 5) / 6;
    CGRect newFrameOne;
    newFrameOne = CGRectMake(space, 50, 55, 55);
    CGRect newFrameTwo;
    newFrameTwo = CGRectMake(space * 2 + _squareOne.frame.size.width, 50, 55, 55);
    _squareOne.frame = newFrameOne;
    _squareTwo.frame = newFrameTwo;
}

按钮永远不会出现......而且我不知道任何其他方法可以做到这一点......

The buttons never show up... and I don't know any other ways to do this...

有什么帮助吗?

推荐答案

你能试试下面这样的吗?

Could you try something like this below?

更新:这次我已经使用 Xcode 正确编写了代码(之前我是从内存中输入的).

Update: I've written the code properly using Xcode this time (previously I typed it from memory).

最终结果见下方截图.

@interface ViewController : UIViewController

@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@property (nonatomic, strong) UIButton *btn3;
@property (nonatomic, strong) UIButton *btn4;
@property (nonatomic, strong) UIButton *btn5;


@end

视图控制器实现文件

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self initViews];
    [self initConstraints];
}


-(void)initViews
{
    self.btn1 = [[UIButton alloc] init];
    self.btn1.backgroundColor = [UIColor grayColor];

    self.btn2 = [[UIButton alloc] init];
    self.btn2.backgroundColor = [UIColor grayColor];

    self.btn3 = [[UIButton alloc] init];
    self.btn3.backgroundColor = [UIColor grayColor];

    self.btn4 = [[UIButton alloc] init];
    self.btn4.backgroundColor = [UIColor grayColor];

    self.btn5 = [[UIButton alloc] init];
    self.btn5.backgroundColor = [UIColor grayColor];



    [self.view addSubview:self.btn1];
    [self.view addSubview:self.btn2];
    [self.view addSubview:self.btn3];
    [self.view addSubview:self.btn4];
    [self.view addSubview:self.btn5];
}

-(void)initConstraints
{
    self.btn1.translatesAutoresizingMaskIntoConstraints = NO;
    self.btn2.translatesAutoresizingMaskIntoConstraints = NO;
    self.btn3.translatesAutoresizingMaskIntoConstraints = NO;
    self.btn4.translatesAutoresizingMaskIntoConstraints = NO;
    self.btn5.translatesAutoresizingMaskIntoConstraints = NO;

    id views = @{
                 @"btn1": self.btn1,
                 @"btn2": self.btn2,
                 @"btn3": self.btn3,
                 @"btn4": self.btn4,
                 @"btn5": self.btn5
                 };

    // horizontal constraints

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[btn1]-5-[btn2(==btn1)]-5-[btn3(==btn1)]-5-[btn4(==btn1)]-5-[btn5(==btn1)]-5-|" options:0 metrics:nil views:views]];

    // vertical constraints
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btn1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:5.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btn2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btn1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btn3 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btn1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btn4 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btn1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.btn5 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.btn1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
}

结果

这就是我得到的.

Result

This is what I get.

你指的是什么长黑条,电池指示灯?

What long black strip are you referring to, the battery indicator?

这篇关于将 UIButton 定位在屏幕顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆