使UIButton占屏幕大小的百分比

时间:2022-11-11 11:48:12

I noticed certain button sizes look great on one the iPhone 5 simulator but do not look as good on the iPhone 6 simulator and this is because the heights or constraints that I place on the UIButtons end up leaving a lot of blank space down the bottom of my App Screen.

我注意到iPhone 5模拟器上的某些按钮尺寸看起来很棒,但在iPhone 6模拟器上看起来不太好,这是因为我放在UIButton上的高度或约束最终会在底部留下很多空白区域。我的应用程序屏幕

I would like to have a button that is 40% of the screen size regardless of what device I am simulating on.

我希望有一个40%的屏幕大小的按钮,无论我在模拟什么设备。

Any ideas on how to make the size of the button stay 40% of the screen size regardless of the device?

无论设备如何,关于如何使按钮大小的任何想法都保持在屏幕尺寸的40%?

3 个解决方案

#1


9  

  1. Ctrl drag from button to superview and select Equal Widths
  2. Ctrl从按钮拖动到superview并选择Equal Widths

  3. Open Size Inspector edit Equal Widths constraint and set multiplier to 0.4. And you will see something like this: 使UIButton占屏幕大小的百分比

    Open Size Inspector编辑Equal Widths约束并将乘数设置为0.4。你会看到这样的事情:

  4. Add missing constraints and update frames.

    添加缺少的约束和更新帧。

使UIButton占屏幕大小的百分比

#2


1  

You can't set a constraint to be 40% of the screen height. However, if the button always has a 20px leading and trailing to the superview you could use that width and set an aspect ratio height.

您不能将约束设置为屏幕高度的40%。但是,如果按钮始终具有20px前导和尾随超视图,则可以使用该宽度并设置宽高比高度。

Another way could be to use UIScreen.mainScreen().bounds.size.height * 0.4 for your button height.

另一种方法是使用UIScreen.mainScreen()。bounds.size.height * 0.4作为按钮高度。

A third way is to use the button's superview to set the height. Assuming that your button is a direct child of a UIViewController's view: view.bounds.size.height * 0.4.

第三种方法是使用按钮的超视图来设置高度。假设您的按钮是UIViewController视图的直接子节点:view.bounds.size.height * 0.4。

There's probably a bunch of other ways to do this as well but none of them will involve setting the height to an actual percentage as far as I'm aware.

可能还有很多其他方法可以做到这一点,但据我所知,它们都不会涉及将高度设置为实际百分比。

#3


0  

This function gives you the bounds of the screen

此功能为您提供屏幕边界

var bounds = UIScreen.mainScreen().bounds

var bounds = UIScreen.mainScreen()。bounds

And then you can set the button width multipliying 0.4 x bounds.size.width

然后你可以设置按钮宽度乘以0.4 x bounds.size.width

Regards

#1


9  

  1. Ctrl drag from button to superview and select Equal Widths
  2. Ctrl从按钮拖动到superview并选择Equal Widths

  3. Open Size Inspector edit Equal Widths constraint and set multiplier to 0.4. And you will see something like this: 使UIButton占屏幕大小的百分比

    Open Size Inspector编辑Equal Widths约束并将乘数设置为0.4。你会看到这样的事情:

  4. Add missing constraints and update frames.

    添加缺少的约束和更新帧。

使UIButton占屏幕大小的百分比

#2


1  

You can't set a constraint to be 40% of the screen height. However, if the button always has a 20px leading and trailing to the superview you could use that width and set an aspect ratio height.

您不能将约束设置为屏幕高度的40%。但是,如果按钮始终具有20px前导和尾随超视图,则可以使用该宽度并设置宽高比高度。

Another way could be to use UIScreen.mainScreen().bounds.size.height * 0.4 for your button height.

另一种方法是使用UIScreen.mainScreen()。bounds.size.height * 0.4作为按钮高度。

A third way is to use the button's superview to set the height. Assuming that your button is a direct child of a UIViewController's view: view.bounds.size.height * 0.4.

第三种方法是使用按钮的超视图来设置高度。假设您的按钮是UIViewController视图的直接子节点:view.bounds.size.height * 0.4。

There's probably a bunch of other ways to do this as well but none of them will involve setting the height to an actual percentage as far as I'm aware.

可能还有很多其他方法可以做到这一点,但据我所知,它们都不会涉及将高度设置为实际百分比。

#3


0  

This function gives you the bounds of the screen

此功能为您提供屏幕边界

var bounds = UIScreen.mainScreen().bounds

var bounds = UIScreen.mainScreen()。bounds

And then you can set the button width multipliying 0.4 x bounds.size.width

然后你可以设置按钮宽度乘以0.4 x bounds.size.width

Regards