提问人:dilshad haidari 提问时间:8/29/2015 更新时间:8/30/2015 访问量:1456
以编程方式在视图中央设置自动布局 iOS
set auto layout programmatically in centre of the view iOS
问:
我是iOS的新手,使用整个屏幕的模糊效果视图,并希望以编程方式为视图中心的Textfield设置自动布局,我的代码是:
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.请更正文本字段相同x=42,y=150位置的代码 2.Texfield位于视野的中心。
如果有人可以帮助我使用正确的代码,请提前致谢
答:
0赞
michaelrccurtis
8/29/2015
#1
您必须先将视图添加到超级视图中,然后才能以您尝试的方式设置约束,即
[[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];
对于水平中心,同样适用于垂直中心。
0赞
Tabish Sohail
8/30/2015
#2
EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y);
使用这个
评论