以编程方式在视图中心设置自动布局 iOS
set auto layout programmatically in centre of the view iOS
我是 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',原因:“无法解析约束格式:
无法解释“|”字符,因为相关视图没有父视图
H:|-42-[EmailTextField(240)]
我想要两个解决方案-
1.Please 更正文本字段相同 x=42,y=150 位置的代码
2.Texfield 在视图的中心。
如果有人能帮助我提供正确的代码,请提前致谢
您必须先将视图添加到父视图,然后才能按照您尝试过的方式设置约束,即
[[vibrancyEffectView contentView] addSubview:EmailTextField];
需要在您的 constraintsWithVisualFormat:options:metrics:views:
电话之前。
这里有一个很好的 link 解释格式代码: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];
水平居中,垂直居中也类似。
EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y);
使用这个
我是 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',原因:“无法解析约束格式: 无法解释“|”字符,因为相关视图没有父视图 H:|-42-[EmailTextField(240)]
我想要两个解决方案- 1.Please 更正文本字段相同 x=42,y=150 位置的代码 2.Texfield 在视图的中心。
如果有人能帮助我提供正确的代码,请提前致谢
您必须先将视图添加到父视图,然后才能按照您尝试过的方式设置约束,即
[[vibrancyEffectView contentView] addSubview:EmailTextField];
需要在您的 constraintsWithVisualFormat:options:metrics:views:
电话之前。
这里有一个很好的 link 解释格式代码: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];
水平居中,垂直居中也类似。
EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y);
使用这个