简体   繁体   English

在视图iOS中心以编程方式设置自动布局

[英]set auto layout programmatically in centre of the view iOS

i am new to iOS using blur effect view for whole screen and want to set the auto layout programatically for textfield in centre of the view and my code is : 我是iOS新手,在整个屏幕上使用模糊效果视图,并且想以编程方式为视图中心的文本字段设置自动布局,我的代码是:

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[blurEffectView setFrame:self.view.bounds];
[self.view addSubview:blurEffectView];

// Vibrancy effect
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
[vibrancyEffectView setFrame:self.view.bounds];

UITextField *EmailTextField = [[UITextField alloc] init;
vibrancyEffectView.translatesAutoresizingMaskIntoConstraints = NO;
NSArray *heightConstraint = [NSLayoutConstraint      constraintsWithVisualFormat:@"V:[EmailTextField(30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(EmailTextField)];
NSArray *widthConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[EmailTextField(240)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(EmailTextField)];
NSArray *xConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-42-[EmailTextField(240)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(EmailTextField)];
NSArray *yConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:-150-[EmailTextField(30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(EmailTextField)];
EmailTextField.textColor = [UIColor redColor];
EmailTextField.font = [UIFont fontWithName:@"Helvetica" size:16];
EmailTextField.backgroundColor=[UIColor clearColor];
EmailTextField.borderStyle = UITextBorderStyleRoundedRect;
[EmailTextField.layer setCornerRadius:10.0f];
EmailTextField.layer.borderWidth = 1.0;
EmailTextField.layer.borderColor = [UIColor whiteColor].CGColor;
EmailTextField.placeholder=@"Email ID";
EmailTextField.textAlignment= NSTextAlignmentCenter;
[[vibrancyEffectView contentView] addSubview:EmailTextField];
[[blurEffectView contentView] addSubview:vibrancyEffectView];
[vibrancyEffectView addConstraints:widthConstraint];
[vibrancyEffectView addConstraints:heightConstraint];
[vibrancyEffectView addConstraints:xConstraint];
[vibrancyEffectView addConstraints:yConstraint];

but gives error : 但给出错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse constraint format: Unable to interpret '|' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无法解析约束格式:无法解释'|' character, because the related view doesn't have a superview H:|-42-[EmailTextField(240)] 字符,因为相关视图没有超级视图H:| -42- [EmailTextField(240)]

I want two solutions- 1.Please correct the code for the same x=42,y=150 positions of the textfield 2.Texfield in centre of the view. 我想要两种解决方案-1.请更正文本字段的x = 42,y = 150位置的代码2.视图中心的文本字段。

Thanks in advance if any one could help me with the correct code 在此先感谢您,如果有人可以帮助我提供正确的代码

You must add your views to the superview before being able to set constraints in the way you have tried to, ie 您必须先将视图添加到超级视图,然后才能以尝试的方式设置约束,即

[[vibrancyEffectView contentView] addSubview:EmailTextField];

Needs to be before your constraintsWithVisualFormat:options:metrics:views: calls. 需要在您的constraintsWithVisualFormat:options:metrics:views:调用之前。

Here is a nice link explaining the format code well: http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/ 这是一个很好的链接,很好地解释了格式代码: http : //commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/

Also, you don't need to specify separate constraints for the width and height because you specify these in the subsequent constraints on position. 另外,您不需要为宽度和高度指定单独的约束,因为您可以在随后的位置约束中指定这些约束。

If you want to centre the text field in the view, then use the explicit constraint setting method, which is very clear (if more verbose!): 如果要在视图中将文本字段居中,请使用显式约束设置方法,该方法非常明确(如果更冗长!):

[NSLayoutConstraint constraintWithItem:EmailTextField
                             attribute:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual                                                             
                                toItem:[vibrancyEffectView contentView]
                             attribute:NSLayoutAttributeCenterX 
                            multiplier:1 
                              constant:0];

for the horizontal centre and similarly for the vertical centre. 用于水平中心,类似地用于垂直中心。

EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y); Use this 用这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM