Ios 应用一些约束时,UITextfield将变得不可编辑
当我使用Ios 应用一些约束时,UITextfield将变得不可编辑,ios,objective-c,uilabel,nslayoutconstraint,Ios,Objective C,Uilabel,Nslayoutconstraint,当我使用UITextfield、UILabel和UIButton时,发生了一些奇怪的事情。我正在应用约束,以便标签和文本字段集中在ui视图中。按钮钉在底部。当我运行此代码时,UITextField变得不可编辑。有人能告诉我下面的代码出了什么问题吗? 有时UILabel位于文本字段的顶部。也检查一下。你介意详细说明一下吗?谢谢 UIView* topLayout = [[UIView alloc]init]; [topLayout setTranslatesAutoresizingMaskInt
UITextfield
、UILabel
和UIButton
时,发生了一些奇怪的事情。我正在应用约束,以便标签
和文本字段
集中在ui视图
中。按钮钉在底部。当我运行此代码时,UITextField
变得不可编辑。有人能告诉我下面的代码出了什么问题吗?
有时UILabel位于文本字段的顶部。也检查一下。你介意详细说明一下吗?谢谢
UIView* topLayout = [[UIView alloc]init];
[topLayout setTranslatesAutoresizingMaskIntoConstraints:NO];
topLayout.backgroundColor = [UIColor whiteColor];
[self.view addSubview: topLayout];
NSArray *topLayoutHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[parentView(==mainView)]" options:0 metrics:0 views:@{@"parentView": topLayout, @"mainView":self.view}];
NSArray *topLayoutVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[parentView]|" options:0 metrics:0 views:@{@"parentView": topLayout}];
[self.view addConstraints:topLayoutHConstraints];
[self.view addConstraints:topLayoutVConstraints];
UIView *childViewLayout = [[UIView alloc]init];
childViewLayout.backgroundColor = [UIColor yellowColor];
[childViewLayout setTranslatesAutoresizingMaskIntoConstraints:NO];
[topLayout addSubview:childViewLayout];
NSLayoutConstraint *childViewControlsHConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
NSLayoutConstraint *childViewControlsVConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *childViewControlsHtConstraints = [NSLayoutConstraint constraintWithItem:childViewLayout attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[topLayout addConstraint:childViewControlsHConstraints];
[topLayout addConstraint:childViewControlsVConstraints];
//[topLayout addConstraint:childViewControlsHtConstraints];
// Instruction field
UILabel* instructionFieldLbl = [[UILabel alloc]init];
[childViewLayout addSubview:instructionFieldLbl];
[instructionFieldLbl setText:@"Please check your SMS or E-Mail for the 4 digit actiation code. Please enter the same to activat your account"];
UIFont *instructionFieldLblFont = [UIFont fontWithName:@"Arial-BoldMT" size:13];
[instructionFieldLbl setFont:instructionFieldLblFont];
instructionFieldLbl.numberOfLines = 0;
[instructionFieldLbl sizeToFit];
[instructionFieldLbl setTextColor:[UIColor grayColor]];
instructionFieldLbl.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *instructionFieldHConstraint = [NSLayoutConstraint constraintWithItem:instructionFieldLbl attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationLessThanOrEqual toItem:childViewLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0f];
//NSArray *instructionFieldHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[instructionFieldLbl]-10-|" options:0 metrics:nil views:@{@"instructionFieldLbl": instructionFieldLbl}];
[childViewLayout addConstraint:instructionFieldHConstraint];
//[childViewLayout addConstraints:instructionFieldHConstraints];
dispatch_async(dispatch_get_main_queue(), ^{
instructionFieldLbl.preferredMaxLayoutWidth = self.view.bounds.size.width;
});
// Activation field
self->m_ObjActivationField = [[JVFloatLabeledTextField alloc]init];
[childViewLayout addSubview:self->m_ObjActivationField];
self->m_ObjActivationField.borderStyle = UITextBorderStyleRoundedRect;
self->m_ObjActivationField.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjActivationField.placeholder = [NSString stringWithFormat:@"Enter the activtion code"];
NSLayoutConstraint *activationHorzConstraint = [NSLayoutConstraint constraintWithItem:self->m_ObjActivationField attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:childViewLayout attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[childViewLayout addConstraint:activationHorzConstraint];
NSDictionary *myControlViews = @{
@"instructionFieldLbl": instructionFieldLbl,
@"activationField": self->m_ObjActivationField
};
NSArray *myControlViewsVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[instructionFieldLbl]-10-[activationField]|" options:0 metrics:nil views:myControlViews];
[childViewLayout addConstraints:myControlViewsVConstraints];
// Submit button
self->m_ObjActivationBut = [[UIButton alloc]init];
[topLayout addSubview:self->m_ObjActivationBut];
[self->m_ObjActivationBut setTitle: [NSString stringWithFormat:@"Activate"] forState:UIControlStateNormal];
self->m_ObjActivationBut.backgroundColor = [UIColor redColor];
[self->m_ObjActivationBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self->m_ObjActivationBut.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjActivationBut.layer.cornerRadius = 10;
self->m_ObjActivationBut.clipsToBounds = YES;
NSDictionary *topViews = @{
@"childViewLayout": childViewLayout,
@"activationButton": self->m_ObjActivationBut
};
//NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[activationButton(40)]-10-|" options:0 metrics:nil views:topViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[activationButton]-|" options:0 metrics:nil views:@{@"activationButton": self->m_ObjActivationBut}];
NSLayoutConstraint *myTopVConstraints = [NSLayoutConstraint constraintWithItem:self->m_ObjActivationBut attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:topLayout attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0f];
[topLayout addConstraint:myTopVConstraints];
[topLayout addConstraints:myTopHConstraints];