【问题标题】:Positioning UIButtons at top of Screen将 UIButtons 定位在屏幕顶部
【发布时间】:2014-10-22 01:53:51
【问题描述】:

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

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

- (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;
}

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

有什么帮助吗?

【问题讨论】:

    标签: ios objective-c uibutton


    【解决方案1】:

    你能试试下面这样的方法吗?

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

    最终结果见下方截图。

    查看控制器头文件

    @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]];
    }
    

    结果

    这就是我得到的。

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

    【讨论】:

    • 它可以工作,但是按钮右侧有一条黑色长条。你能改变你的代码来修复它吗?
    • 不确定你所说的黑条是什么意思,看我上面的截图应该是什么样子。
    • 我使用在 h 文件中初始化的 55x55 uibuttons。它们在按钮的右侧和底部都有一条黑色条带。宽度约为 20 像素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多