在我的应用程序中,我使用Autolayouts在主视图控制器上添加了一个文本字段和一个按钮。
使用以下代码,UItextfild和Button之间不应用水平间距。我怎么可能做错了?
NSDictionary * views1 = NSDictionaryOfVariableBindings(RoundTripDateTextField,RoundTripButton);
[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[RoundTripDateTextField]-8-[RoundTripButton(30)]-0-|" options:0 metrics:nil views:views1]];
[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[RoundTripDateTextField(30)]" options:0 metrics:nil views:views1]];
[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[RoundTripButton(30)]" options:0 metrics:nil views:views1]];
答案 0 :(得分:0)
您的约束中存在一些问题。我已经使用相同的约束编写了一个片段,只是稍微调整了一下:
UIView *RoundTripBackGround = [[UIView alloc] init];
RoundTripBackGround.translatesAutoresizingMaskIntoConstraints = NO;
RoundTripBackGround.backgroundColor = [UIColor yellowColor];
[self.view addSubview:RoundTripBackGround];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[RoundTripBackGround]|"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(RoundTripBackGround)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(20)-[RoundTripBackGround]"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(RoundTripBackGround)]];
UITextField *RoundTripDateTextField = [[UITextField alloc] init];
RoundTripDateTextField.translatesAutoresizingMaskIntoConstraints = NO;
RoundTripDateTextField.backgroundColor = [UIColor blueColor];
[RoundTripBackGround addSubview:RoundTripDateTextField];
UIButton *RoundTripButton = [UIButton buttonWithType:UIButtonTypeSystem];
RoundTripButton.translatesAutoresizingMaskIntoConstraints = NO;
RoundTripButton.backgroundColor = [UIColor greenColor];
[RoundTripBackGround addSubview:RoundTripButton];
NSDictionary * views1 = NSDictionaryOfVariableBindings(RoundTripDateTextField,RoundTripButton);
[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[RoundTripDateTextField]-8-[RoundTripButton(30)]|"
options:0 metrics:nil
views:views1]];
[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[RoundTripDateTextField(30)]|"
options:0 metrics:nil
views:views1]];
[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[RoundTripButton(30)]|"
options:0 metrics:nil
views:views1]];
需要注意的事项: