按钮的比例调整大小我需要布置5个按钮作为该图像中:在UIStackView
我需要他们按比例调整在不同设备上。
我有一个方法设置了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;
此代码返回空白视图。我是否错误地使用了方法?
我无法弄清楚我做错了什么。我需要在目标c中以编程方式完成此操作。
感谢
请详细说明“我需要他们在不同设备上按比例调整。”就像“如果堆栈视图高度是xxx按钮偏移量是xx,那么按钮高度应该是xxx”,并且与宽度类似。 – BangOperator
@BangOperator我需要它看起来像是在所有设备上提供的图像,我需要以编程方式而不是IB。因此,在横向模式下,stackView.height = screen.height从顶部和底部减去20个点,button.height =每个按钮之间5点间距的stackView的1/5。和stackView.width = screen.width和1/4距离左边缘20点,button.width = stackView.width。希望有道理 – sharp