我是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];
但是错误:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无法解析约束格式: 无法解释'|'字符,因为相关视图没有superview H:| -42- [EmailTextField(240)]
我想要两个解决方案 - 1.请更正文本字段的相同x = 42,y = 150个位置的代码 2.Texfield在视图的中心。
如果有人能帮助我使用正确的代码,请提前致谢
答案 0 :(得分:0)
您必须先将视图添加到超级视图,然后才能按照您尝试的方式设置约束,即
[[vibrancyEffectView contentView] addSubview:EmailTextField];
需要在constraintsWithVisualFormat:options:metrics:views:
来电之前。
这是一个很好的链接,很好地解释了格式代码:http://commandshift.co.uk/blog/2013/01/31/visual-format-language-for-autolayout/
此外,您不需要为宽度和高度指定单独的约束,因为您在后续的位置约束中指定了这些约束。
如果你想在视图中居中文本字段,那么使用显式约束设置方法,这非常清楚(如果更详细!):
[NSLayoutConstraint constraintWithItem:EmailTextField
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:[vibrancyEffectView contentView]
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0];
表示水平中心,类似表示垂直中心。
答案 1 :(得分:0)
EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y);
使用此