简体   繁体   English

按比例调整UIStackView中按钮的大小

[英]Proportional resizing of buttons in UIStackView

I need to layout 5 buttons as in this image: 我需要布局5个按钮,如下图所示: 图1

I need them to resize proportionally on different devices. 我需要它们在不同设备上按比例调整大小。

I have this code in a method which sets up a view with 5 buttons. 我的代码中有一种方法可以设置5个按钮的视图。

UIStackView *stackView = [[UIStackView alloc]init];

// Add buttons to stackView (buttons are initialised)
[stackView addArrangedSubview:_button1];
[stackView addArrangedSubview:_button2];
[stackView addArrangedSubview:_button3];
[stackView addArrangedSubview:_button4];
[stackView addArrangedSubview:_button5];

stackView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:stackView];

stackView.axis = UILayoutConstraintAxisVertical;
stackView.distribution = UIStackViewDistributionFillEqually;
stackView.spacing = 5;

//Layout Constraints for stackView
[stackView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = true;
[stackView.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = true;

// 80% of screen height should be used for buttons
CGFloat screenHeightAvailableForButtons = 80/100;
// Calculate multiplier for height of the buttons (5 buttons)
CGFloat buttonMultiplier = screenHeightAvailableForButtons /5;
// 25% of screen width should be used for buttons
CGFloat screenWidthMultiplier =  25/100;

[_button1.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier].active = true;
[_button1.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true;

[_button2.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true;
[_button2.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true;

[_button3.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true;
[_button3.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true;

[_button4.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true;
[_button4.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true;

[_button5.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier: screenWidthMultiplier ].active = true;
[_button5.heightAnchor constraintEqualToAnchor: self.view.heightAnchor multiplier: buttonMultiplier].active = true;

This code returns a blank view. 此代码返回空白视图。 Am I using the methods incorrectly? 我使用方法不正确吗?
I can't work out what I am doing wrong. 我无法弄清楚我在做什么错。 I need this done programmatically in objective c. 我需要在目标c中以编程方式完成此操作。

thanks 谢谢

For your case, you don't need to set the constraints inside the stack view for buttons. 对于您的情况,您不需要在堆栈视图内为按钮设置约束。 Stack view will handle this automatically. 堆栈视图将自动处理此问题。

Try this 尝试这个

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

    UIStackView *stackView = [[UIStackView alloc]initWithFrame:self.view.bounds];
    stackView.backgroundColor = [UIColor darkGrayColor];
    _button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button1 setTitle:@"Button 1" forState:UIControlStateNormal];
    _button1.translatesAutoresizingMaskIntoConstraints = NO;
    _button1.backgroundColor = [UIColor lightGrayColor];

    _button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button2 setTitle:@"Button 2" forState:UIControlStateNormal];
    _button2.translatesAutoresizingMaskIntoConstraints = NO;
    _button2.backgroundColor = [UIColor lightGrayColor];

    _button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button3 setTitle:@"Button 3" forState:UIControlStateNormal];
    _button3.translatesAutoresizingMaskIntoConstraints = NO;
    _button3.backgroundColor = [UIColor lightGrayColor];


    _button4 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button4 setTitle:@"Button 4" forState:UIControlStateNormal];
    _button4.translatesAutoresizingMaskIntoConstraints = NO;
    _button4.backgroundColor = [UIColor lightGrayColor];

    _button5 = [UIButton buttonWithType:UIButtonTypeSystem];
    [_button5 setTitle:@"Button 5" forState:UIControlStateNormal];
    _button5.translatesAutoresizingMaskIntoConstraints = NO;
    _button5.backgroundColor = [UIColor lightGrayColor];

    // Add buttons to stackView (buttons are initialised)
    [stackView addArrangedSubview:_button1];
    [stackView addArrangedSubview:_button2];
    [stackView addArrangedSubview:_button3];
    [stackView addArrangedSubview:_button4];
    [stackView addArrangedSubview:_button5];

    stackView.translatesAutoresizingMaskIntoConstraints = false;
    [self.view addSubview:stackView];

    stackView.axis = UILayoutConstraintAxisVertical;
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 5;

    //Layout Constraints for stackView
    [stackView.layoutMarginsGuide.topAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.topAnchor].active = true;
    [stackView.layoutMarginsGuide.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor].active = true;
    // 25% of screen width should be used for buttons
    [stackView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:25.0/100.0].active = true;
    [stackView.layoutMarginsGuide.centerXAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.centerXAnchor].active = true;

}

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

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