Xcode IB:UIButton 隐藏但仍有按钮空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26298961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode IB: UIButton hidden but have still buttons space
提问by Stefan van de Laarschot
I have the following issue in my iPhone app.
I have 4 buttons in my IB also linked to my UIViewController
(IBOutlet
)
When I for example hide the second button wich is AfvalSoortenwith [self.btnAfvalSoorten setHidden:YES];
then it dissapears, that's what I want, but I still got the button space when I debug the app on the simulator.
我的 iPhone 应用程序中存在以下问题。我的 IB 中有 4 个按钮也链接到我的UIViewController
( IBOutlet
) 例如,当我隐藏第二个按钮时,它是AfvalSoorten,[self.btnAfvalSoorten setHidden:YES];
然后它消失了,这就是我想要的,但是当我在模拟器上调试应用程序时,我仍然得到了按钮空间。
How can I get rid of that? Bellow there is an example.
我怎样才能摆脱它?下面是一个例子。
Is there an option on the Storyboard for the buttons ? to clip together?
故事板上有按钮的选项吗?夹在一起?
回答by djromero
You should use autolayout. Otherwise it's a nightmare with the new screen sizes.
您应该使用自动布局。否则,新屏幕尺寸将是一场噩梦。
With autolayout you can do what you ask programmatically: setup the buttons with certain constraints and then when you decide to hide the button remove the constraints that are not needed. It's flexible and powerful but not the easiest way for a beginner.
使用自动布局,您可以以编程方式执行您的要求:设置具有某些约束的按钮,然后当您决定隐藏按钮时删除不需要的约束。它灵活而强大,但对于初学者来说并不是最简单的方法。
One simple way to do it is with additional constraints. For instance, if you have buttons 1, 2 and 3 (see screenshot), and you plan to remove button 2, you can add an extra constraint between 3 and 1:
一种简单的方法是使用额外的约束。例如,如果您有按钮 1、2 和 3(参见屏幕截图),并且您计划删除按钮 2,则可以在 3 和 1 之间添加额外的约束:
That constraint should have less priority (250 in my example) than the others (1000 by default). That mean that the constraint won't be applied when button 2 is in place (with higher priority constraints).
该约束的优先级(在我的示例中为 250)应低于其他约束(默认为 1000)。这意味着当按钮 2 到位(具有更高优先级约束)时,不会应用约束。
Then, remove the button instead of hiding it.
然后,删除按钮而不是隐藏它。
[self.button removeFromSuperview];
When you hide the button it still considered by the layout system to take decisions, and it makes layout more complex. If you want to keep the button around make sure it's using strong
modifier in the property declaration.
当您隐藏按钮时,布局系统仍会考虑做出决定,这会使布局更加复杂。如果您想保留按钮,请确保它strong
在属性声明中使用修饰符。
回答by Alexander Bekert
The modern preferred way of doing so is to use Stack Views. Great tutorial. Requires iOS 9.
这样做的现代首选方法是使用堆栈视图。很棒的教程。需要 iOS 9。
You'll find the icon of stack view in the Auto Layout toolbar at the bottom right of the storyboard canvas.
您将在故事板画布右下方的自动布局工具栏中找到堆栈视图的图标。
回答by Kampai
A better approach for above scenario - You dont need to set any autolayout or frames :)
上述情况的更好方法 -您不需要设置任何自动布局或框架:)
Use UITableView
and create custom cell with UIButtons
in it.
使用UITableView
并UIButtons
在其中创建自定义单元格。
Set
UITableViewCellSelectionStyle
toNone
Here your button background is same for all cell
Create an array with above button titles
When ever you want to hide buttons just remove it from array.
设置
UITableViewCellSelectionStyle
为None
这里所有单元格的按钮背景都是相同的
创建一个带有上述按钮标题的数组
当您想隐藏按钮时,只需将其从数组中删除即可。
回答by pankaj asudani
You had just make it hidden. You have to set the frames according to your need. OR You can set autolayout.
你刚刚把它隐藏了。您必须根据需要设置框架。或者您可以设置自动布局。
回答by Nicolas Manzini
maybe there is a solution with autoLayout in IB but I'm not sure about that. Programmatically you can add all your button to an array in order. and whenever you hide a button or not you loop trough the array of button and each time you find one that is not hidden you set the y coordinate on the frame to a a value and you increment this value by what you need so the next not hidden will be placed according to the last position used.
也许在 IB 中有一个带有 autoLayout 的解决方案,但我不确定。以编程方式,您可以按顺序将所有按钮添加到数组中。每当你隐藏一个按钮或不隐藏按钮时,你都会循环遍历按钮数组,每次找到一个没有隐藏的按钮时,将框架上的 y 坐标设置为 aa 值,然后根据需要增加该值,以便下一个不隐藏将根据最后使用的位置放置。
回答by kabarga
- You can add the buttons programmatically -> you will have the array of Btns and method to add array to the view controller.
- You can play with the Constraints and set Height Constraint for 2nd button when hide it to 0 but all buttons should be connected with the constraints in this case.
- 您可以以编程方式添加按钮 -> 您将拥有 Btns 数组和将数组添加到视图控制器的方法。
- 当将其隐藏为 0 时,您可以使用约束并为第二个按钮设置高度约束,但在这种情况下,所有按钮都应与约束连接。