iOS 等效于 Android View.GONE 可见性模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17869268/
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
iOS equivalent for Android View.GONE visibility mode
提问by elbuild
I'm developing an app for iOS and I'm using the Storyboard with AutoLayout ON. One of my view controllers has a set of 4 buttons, and in certain circumstances i would like to make the first one disappear.
我正在为 iOS 开发一个应用程序,并且我正在使用带有 AutoLayout ON 的 Storyboard。我的一个视图控制器有一组 4 个按钮,在某些情况下我想让第一个按钮消失。
If I use the setHidden:TRUE
method the UIButton become invisible but it still obviously take space in the view, and the result is an "hole" which I've not been able to fill making the remaining UIButton to float towards the top of the main view.
如果我使用该setHidden:TRUE
方法,UIButton 变得不可见,但它显然仍然在视图中占用空间,结果是一个“洞”,我无法填充它,使剩余的 UIButton 浮动到主视图的顶部。
In Android I would have simply used View.GONE
instead of View.INVISIBLE
, but in iOS I'm stuck with this behaviour and I don't want to believe that the only solution is to manually (yes I mean programmatically) move the remaining elements to the top.
在 Android 中,我会简单地使用View.GONE
而不是View.INVISIBLE
,但在 iOS 中我坚持这种行为,我不想相信唯一的解决方案是手动(是的,我的意思是以编程方式)将其余元素移动到顶部。
I thought I would have been able to do it setting some sort of Constraint to make everything as automatic as it is in Android but I've had no luck.
我以为我可以通过设置某种约束来使一切都像在 Android 中一样自动化,但我没有运气。
Before I turn Autolayout OFF, can someone point me to the right direction?
在我关闭自动布局之前,有人能指出我正确的方向吗?
I'm using the IB, but I'm comfortable with programmatic stuff as well.
我正在使用 IB,但我对程序化的东西也很满意。
UPDATE:
更新:
Setting the component height to 0 doesn't help as well.
将组件高度设置为 0 也无济于事。
I tried something like this:
我试过这样的事情:
UIButton *b;
CGRect frameRect = b.frame;
frameRect.size.height = 0;
b.frame = frameRect;
回答by Deniz
Adding a constraint(NSLayoutAttributeHeight) that sets height of the view to 0 worked for me:
添加将视图高度设置为 0 的约束(NSLayoutAttributeHeight)对我有用:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.captchaView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]];
回答by Beyaz
All of answers on this questions are inefficient.
Best way to equvailent of Android setVisibility:Gone method on iOS is that StackView
first select components then in editor, embed in, Stack View,
关于这个问题的所有答案都是低效的。等效于 Android setVisibility:Gone 方法在 iOS 上的最佳方法是StackView
首先选择组件,然后在编辑器中,嵌入到堆栈视图中,
connect new stack view with IBOutlet, then:
使用 IBOutlet 连接新的堆栈视图,然后:
hidden:
隐:
UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0];
firstView.hidden = YES;
visibility:
能见度:
UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0];
firstView.hidden = NO;
as using stack view, all constraints will be keeped!
使用堆栈视图时,将保留所有约束!
回答by Ajinkya Patil
add a height constraint to your view as follows:
then make an outlet for the height constraint in your viewController file as follows:
然后在您的 viewController 文件中为高度约束创建一个出口,如下所示:
& then in your viewDidLoad method change constraint height to 0 as follows:
然后在您的 viewDidLoad 方法中将约束高度更改为 0,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
nsLcButtonHeight.constant = 0
}
回答by Md Imran Choudhury
To achieve Androids.GONE functionality on iOS is to use a UIStackView. After that hide the child by position. (Apples documentation)
在 iOS 上实现 Androids.GONE 功能是使用 UIStackView。之后,按位置隐藏孩子。(苹果文档)
SWIFT 4:
快速 4:
let firstView = cell.rootStackView.arrangedSubviews[0]
firstView.isHidden = true // first view gone
It's a table cell example, just put both insides Stack view
and get item for GONE
the child.
这是一个表格单元格示例,只需将两个内部都放入Stack view
并为GONE
孩子获取项目。
回答by mechdon
What you can do is to group your views under a stack view. Then when you hide a particular view, the remaining views will be shifted automatically to fill the space.
您可以做的是将您的视图分组在堆栈视图下。然后,当您隐藏特定视图时,剩余的视图将自动移动以填充空间。
You may want to check out the Apple Documentation on Stack Views: https://developer.apple.com/reference/uikit/uistackview
您可能需要查看堆栈视图上的 Apple 文档:https: //developer.apple.com/reference/uikit/uistackview
or online tutorials such as: https://www.appcoda.com/stack-views-intro/
或在线教程,例如:https: //www.appcoda.com/stack-views-intro/
回答by wyu
This question is pretty old but the closet thing I've found is setting additional constraints (so the views around the "gone" view know what to do once it's missing)
这个问题已经很老了,但我发现的壁橱里的东西是设置额外的约束(所以“消失”视图周围的视图知道一旦丢失该怎么办)
A which you want to be A
| after setting B to gone |
B C
|
C
- Set a lower priority (750) constraint from C to A.
- Add B's top and bottom constraints (or left and right if you want your view to collapse horizontally) to an NSLayoutConstraint array
bConstraints
. Do this by:- Control click and drag from the constraint to the ViewController
- Change Connection from Outlet to Outlet Collection
- For name put
bConstraints
. - Hit connect. This will create an
@IBOutlet var bConstraints: [NSLayoutConstraint]!
in your ViewController - To add additional constraints: drag from the constraint in Storyboard to the @IBOutlet variable name
Then hide B
B.hidden = true NSLayoutConstraint.deactivateConstraints(bConstraints)
To unhide
B.hidden = false NSLayoutConstraint.activateConstraints(bConstraints)
- 将较低优先级 (750) 约束从 C 设置为 A。
- 将 B 的顶部和底部约束(如果您希望视图水平折叠,则向左和向右)添加到 NSLayoutConstraint 数组
bConstraints
。这样做:- 控制单击并从约束拖动到 ViewController
- 将连接从出口更改为出口集合
- 为名称放
bConstraints
。 - 点击连接。这将
@IBOutlet var bConstraints: [NSLayoutConstraint]!
在您的 ViewController 中创建一个 - 添加其他约束:从 Storyboard 中的约束拖动到 @IBOutlet 变量名称
然后隐藏B
B.hidden = true NSLayoutConstraint.deactivateConstraints(bConstraints)
取消隐藏
B.hidden = false NSLayoutConstraint.activateConstraints(bConstraints)
Obviously the more and more views you have the more complex this grows, as you need additional constraints from each view
显然,您拥有的视图越多,它就越复杂,因为您需要每个视图的额外约束
回答by poyo fever.
https://github.com/tazihosniomar/LayoutManager
https://github.com/tazihosniomar/LayoutManager
i hope it will help you .
我希望它会帮助你。
回答by iphondroid
1) If your buttons are placed vertically then you must set the height
to 0 and in case of horizontally placed buttons, try setting width
to 0 or you can set both to 0.
1) 如果您的按钮是垂直放置的,那么您必须将 设置height
为 0,如果是水平放置的按钮,请尝试设置width
为 0,或者您可以将两者都设置为 0。
OR
或者
2) you could try this approach which sets button2
on top of button1
:
2)您可以尝试这种方法,它设置button2
在button1
:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:@"hello" forState:UIControlStateNormal];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"world" forState:UIControlStateNormal];
[button1 sizeToFit]; [button2 sizeToFit];
[button2 setFrame:CGRectMake(button1.frame.origin.x, button1.frame.origin.y, button2.frame.size.width, button2.frame.size.height)];
[self.view addSubview:button1];
[self.view addSubview:button2];
}
回答by Katlu
As my research has shown, not even AutoLayout can help you. You have to manually replace the views affected by the optionally shown component (in my case, all the views to the bottom of the optional view, but you I am sure you can adapt this to handle all the buttons to the right of your optional button):
正如我的研究表明的那样,即使是 AutoLayout 也帮不了你。您必须手动替换受可选显示组件影响的视图(在我的情况下,所有视图都位于可选视图的底部,但我确定您可以调整它以处理可选按钮右侧的所有按钮):
- (IBAction)toggleOptionalView:(id)sender {
if (!_expanded) {
self.optionalView.frame = CGRectMake(self.optionalView.frame.origin.x, self.optionalView.frame.origin.y, self.optionalView.frame.size.width, _optionalHeight);
self.bottomView.frame = CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y+_optionalHeight, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
_expanded = YES;
} else {
self.optionalView.frame = CGRectMake(self.optionalView.frame.origin.x, self.optionalView.frame.origin.y, self.optionalView.frame.size.width, 0);
self.bottomView.frame = CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y-_optionalHeight, self.bottomView.frame.size.width, self.bottomView.frame.size.height);
_expanded = NO;
}
}
It is advisable not to hard-code the height/width of the optional components, otherwise your code breaks every time you edit the XIB/Storyboard. I have a field float _optionalHeight which I set in viewDidLoad, so it is always up to date.
建议不要对可选组件的高度/宽度进行硬编码,否则每次编辑 XIB/故事板时您的代码都会中断。我在 viewDidLoad 中设置了一个字段 float _optionalHeight,因此它始终是最新的。
回答by The4thIceman
Building off the answer provided by Deniz, here is a solution using constraints in Swift
基于 Deniz 提供的答案,这是一个在 Swift 中使用约束的解决方案
For example: If you have 3 views, A_view B_view and C_view vertically aligned in that order and you want to "Hide" B and also adjust the difference, add a constraint
例如:如果您有 3 个视图,A_view B_view 和 C_view 以该顺序垂直对齐,并且您想“隐藏”B 并调整差异,请添加约束
B_view.removeFromSuperView()
var constr = NSLayoutConstraint(item: C_view,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: A_view,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1,
constant: 20)
view.addConstraint(constr)
constant is (in this case) the amount of vertical space between C_view and A_view
常量是(在这种情况下)C_view 和 A_view 之间的垂直空间量