【发布时间】:2014-08-06 14:14:41
【问题描述】:
我有一个 UIView 的 xib 文件,其中有 2 个按钮。像这样 :
Button1 将在显示时隐藏。当它被隐藏时,我希望其他按钮占用整个空间。
我还希望视图占据所有可用空间(也可以管理方向更改)。在 IB 中查看,我选择了大小为Freeform,方向为Portrait。
为此,我为两个按钮添加了约束,将 0 设置为顶部、底部和左/右。但是有了这个,当方向改变时,它们是我不想要的两个按钮之间的间隙。
我尝试了很多方法,但无法处理上述两个问题。两者也是相互关联的。如果 Button1 是隐藏的,那么也应该将左约束添加到其他按钮。现在,我已经移除了所有限制,所以在横向上它并没有利用全部空间。
您能否告诉我如何处理这些相互关联的问题。
更新 向视图 (UIView) 类添加了以下方法。像这样添加它对我来说是新的,参考编写了代码来添加两个按钮的顶部、底部和左/右设置 0。左边是主要角色 - button1 被隐藏,然后添加什么,然后显示什么。
-(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];
你能帮我解决以上问题吗?
【问题讨论】:
标签: ios objective-c uibutton autolayout