我有一个UIView的xib文件,里面有2个按钮。像这样 :
Button1将在显示时隐藏。当它被隐藏时我想要其他按钮带走整个空间。
我还希望视图占用所有可用空间(也可以管理方向更改)。在IB for view中,我选择了尺寸为Freeform
,方向为Portrait
。
为此,我在两个按钮上添加了约束,设置0到顶部,底部和左/右。但有了这个,当方向改变时,它们是我不想要的两个按钮之间的间隙。
我尝试了很多方法,但我无法处理上述两件事。两者都是相互关联的。如果Button1被隐藏,那么也应该将左约束添加到其他按钮。现在,我已经删除了所有约束,因此在横向上它并没有利用整个空间。
您能告诉我如何处理这些相互关联的问题。
更新 在视图(UIView)类中添加了以下方法。像这样添加它对我来说是新的,参考已编写代码添加到两个按钮上的顶部,底部和左/右设置0。左边是主角色 - 按钮1是隐藏的,然后添加和显示什么。
-(void) addCustomConstriants:(BOOL) hideFirstBtn {
// http://technet.weblineindia.com/mobile/ui-design-of-ios-apps-with-autolayout-using-constraints-programmatically/2/
//NSDictionary *viewsDict = NSDictionaryOfVariableBindings(self.button1, self.button2);
//NSArray *constriaints =
if (hideFirstBtn) {
// ONLY 2ND BUTTON - NO 1ST BUTTON
} else {
// SHOW BOTH BUTTONS
// BUTTON 1
// ADD TOP CONSTRAINT - 0
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0] ];
// ADD BOTTOM - 0
[self addConstraint: [NSLayoutConstraint constraintWithItem:self.button1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0 ] ];
// LEFT
[self addConstraint: [NSLayoutConstraint constraintWithItem:self.button1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0 ] ];
// BUTTON 2
// ADD TOP CONSTRAINT - 0
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.button2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0] ];
// ADD BOTTOM - 0
[self addConstraint: [NSLayoutConstraint constraintWithItem:self.button2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0 ] ];
// ADD RIGHT - 0
[self addConstraint: [NSLayoutConstraint constraintWithItem:self.button2 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0 ] ];
// Width constraint, half of parent view width
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.button1
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// Width constraint, half of parent view width
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.button2
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:0]];
// AFTER ADDING WIDTH ALSO FOR BOTH BUTTONS, WILL HAVE TO ADD X SOME HOW ??? PUZZLE ???
}
}
我相信我还必须为这两个按钮设置宽度。那应该是相同的宽度。如果只有button2,那么那个宽度也必须在这里设置才对。如何为它们添加约束?
这是我从其他屏幕调用此视图的方式: -
vov1 = [visitorOptView objectAtIndex:0];
// note the origin of the frame is (0, 0) since you are adding it to the cell instead of the table view
vov1.frame = CGRectMake(0, 0, selectedCell.frame.size.width, selectedCell.frame.size.height);
btnWidth = selectedCell.frame.size.width;
[vov1.button2 addTarget:self action:@selector(showVisitorDetailsView) forControlEvents:UIControlEventTouchUpInside];
// show/Enable "Start Chat" btn, else Disable it
if (tableView.tag == 0 || tableView.tag ==1) {
// START
[vov1.button1 setTitle:@"Accept" forState:UIControlStateNormal];
[vov1.button1 addTarget:self action:@selector(acceptBtnClicked) forControlEvents:UIControlEventTouchUpInside];
} else if (tableView.tag == 3) {
// CLOSED LIST - HIDE ALL BTNS
[vov1.button1 setHidden:YES];
}
///// HERE addCustomConstraints SHOULD BE CALLED I THINK
// add overlay view to this row
[selectedCell.contentView addSubview:vov1];
你能帮我解决上述问题。
答案 0 :(得分:0)
在这种情况下,您应该按代码添加约束。所以当显示两个按钮时。第一个按钮尾随约束应绑定到前导第二个按钮约束。如果"查看详情"按钮是唯一显示的按钮然后隐藏第一个按钮,该按钮的前导约束将与self.view的前导绑定。
创建一个名为addCustomConstraints
的方法,并在viewDidLoad
中调用它。