android gone了高度还在,iphone - iOS等效于Android View.GONE可见性模块

iphone - iOS等效于Android View.GONE可见性模块

我正在为iOS开发一个应用程序,我正在使用带有AutoLayout ON的Storyboard。 我的一个视图控制器有一组4个按钮,在某些情况下我想让第一个按钮消失。

如果我使用View.GONE方法,UIButton变得不可见,但它仍然显然在视图中占用空间,结果是一个“洞”,我无法填补,使剩余的UIButton浮动到主视图的顶部。

在Android中我会简单地使用View.GONE而不是View.INVISIBLE,但在iOS中我坚持这种行为,我不想相信唯一的解决方案是手动(是的,我的意思是以编程方式)将剩余的元素移到顶部。

我以为我能够设置某种约束来使一切像Android一样自动,但我没有运气。

在我关闭Autolayout之前,有人能指出我正确的方向吗?

我正在使用IB,但我对程序化的东西也很满意。

更新:

将组件高度设置为0也没有用。

我试过这样的事情:

UIButton *b;

CGRect frameRect = b.frame;

frameRect.size.height = 0;

b.frame = frameRect;

13个解决方案

35 votes

添加一个约束(NSLayoutAttributeHeight),将视图的高度设置为0对我有用:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.captchaView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0]];

Deniz answered 2019-06-19T13:22:52Z

15 votes

在视图中添加高度约束,如下所示:

b144b6663fe330e3da38345c28d9dcda.png

然后在viewController文件中为高度约束创建一个出口,如下所示:

a0f72ba3ae6468027f6595441f005118.png

&安培; 然后在viewDidLoad方法中将约束高度更改为0,如下所示:

override func viewDidLoad() {

super.viewDidLoad()

nsLcButtonHeight.constant = 0

}

Ajinkya Patil answered 2019-06-19T13:23:31Z

14 votes

关于这个问题的所有答案都是低效的。平等的Android setVisibility的最佳方法:iOS上的Gone方法是StackView然后在编辑器中首先选择组件,嵌入,Stack View,

使用IBOutlet连接新的堆栈视图,然后:

隐:

UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0];

firstView.hidden = YES;

能见度:

UIView * firstView = self.svViewFontConfigure.arrangedSubviews[0];

firstView.hidden = NO;

使用堆栈视图时,所有约束都会得到保留!

文献

Beyaz answered 2019-06-19T13:24:34Z

6 votes

您可以做的是在堆栈视图下对视图进行分组。 然后,当您隐藏特定视图时,其余视图将自动移动以填充空间。

您可能想要查看堆栈视图上的Apple文档:[https://developer.apple.com/reference/uikit/uistackview]

或在线教程,如:[https://www.appcoda.com/stack-views-intro/]

mechdon answered 2019-06-19T13:25:17Z

3 votes

要在iOS上实现Androids.GONE功能,就是使用UIStackView。 之后,按位置隐藏孩子。 (苹果文件)

SWIFT 4:

let firstView = cell.rootStackView.arrangedSubviews[0]

firstView.isHidden = true // first view gone

这是一个表格单元格的例子,只是将两个内部放入Stack view并获取GONE这个孩子的项目。

e3109b69b6e864c7c8f1081fd1581676.png

Md Imran Choudhury answered 2019-06-19T13:25:54Z

1 votes

1)如果按钮垂直放置,则必须将button2设置为0,如果是水平放置按钮,请尝试将button1设置为0,或者将两者都设置为0。

要么

2)你可以尝试这种方法,在button1之上设置button2:

- (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];

}

iphondroid answered 2019-06-19T13:26:30Z

1 votes

[https://github.com/tazihosniomar/LayoutManager]

我希望它会对你有所帮助。

poyo fever. answered 2019-06-19T13:27:05Z

0 votes

正如我的研究表明,甚至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;

}

}

建议不要对可选组件的高度/宽度进行硬编码,否则每次编辑XIB / Storyboard时代码都会中断。 我有一个字段浮点_optionalHeight我在viewDidLoad中设置,因此它始终是最新的。

Katlu answered 2019-06-19T13:27:40Z

0 votes

您还可以清除页面,或至少清除页面的某些单元格,然后重新定义整个页面。 它速度快,效果很好。 我没有编写代码,但是在我正在进行的这个已有的项目中找到了它。 创建ManagementTableSection和ManagementTableCell类来管理它。 对不起,我无法提供更好的定义代码。

craned answered 2019-06-19T13:28:12Z

0 votes

基于Deniz提供的答案,这里是使用Swift中的约束的解决方案

例如:如果您有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)

常量是(在这种情况下)C_view和A_view之间的垂直空间量

The4thIceman answered 2019-06-19T13:28:56Z

0 votes

我向一个名为“visible”的自定义UIView实现添加了一个新属性,当设置为false时,添加一个约束来折叠视图(我只添加了一个宽度约束,因为我的列表是水平的,但最好的方法可能是添加一个 高度约束为0)。

var visible:Bool = true{

didSet{

if(visible){

clipsToBounds = false;

removeConstraint(hideConstraint!)

}else{

clipsToBounds = true

addConstraint(hideConstraint!)

}

}

}

您需要在视图上初始化零宽度约束并将其添加为字段:

private var hideConstraint:NSLayoutConstraint?

func someInitFunction(){

hideConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 0.0)

...

}

Anthony De Smet answered 2019-06-19T13:29:32Z

0 votes

这个问题已经很老了,但是我发现的壁橱里的东西是设置了额外的约束(所以围绕“消失”视图的视图知道一旦丢失就该做什么)

A which you want to be A

| after setting B to gone |

B C

|

C

设置从C到A的较低优先级(750)约束。

将B的顶部和底部约束(或左侧和右侧,如果您希望视图水平折叠)添加到NSLayoutConstraint数组B.hidden = false

NSLayoutConstraint.activateConstraints(bConstraints)。通过以下方式执行此操作:控制单击并从约束拖动到ViewController

将连接从插座更改为插座集合

名称为B.hidden = false

NSLayoutConstraint.activateConstraints(bConstraints)。

点击连接。 这将在ViewController中创建一个B.hidden = false

NSLayoutConstraint.activateConstraints(bConstraints)

要添加其他约束:从Storyboard中的约束拖动到@IBOutlet变量名称

然后隐藏B.

B.hidden = false

NSLayoutConstraint.activateConstraints(bConstraints)

取消隐藏

B.hidden = false

NSLayoutConstraint.activateConstraints(bConstraints)

显然,越多越多的视图越复杂,因为每个视图都需要额外的约束

wyu answered 2019-06-19T13:31:21Z

-2 votes

setHidden:TRUE / FALSE是与Android View.GONE / VISIBLE最接近的等价物。

如果看不到,视图不一定占用空间!

我已经创建了一个ComboBox-Alike,其中ListView位于其他视图之上。 我只在选择时可见:

2f001b179b46a5db41c00e9a72a8c9b9.png

thpitsch answered 2019-06-19T13:32:06Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值