[英]UIButton inside UIVIew not centered in iPhone app
我实际上是以编程方式设置一个自定义的UIView,它将包含几个UIButton,这样可以正常工作。 但我要做的是将UIButton置于我以编程方式创建的子自定义UIView中。 这是我实际使用的代码
//UIView used as a parent for the blurrEffectView
blurredPowerDown = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
blurredPowerDown.backgroundColor = [UIColor clearColor];
//I'm calling this for applying my custom view on top of other application (yes this is jailbreak development but issue is not related at it at all
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.windowLevel = UIWindowLevelAlert + 2;
[window setHidden:NO];
[window setAlpha:1.0];
[window setBackgroundColor:[UIColor clearColor]];
[window addSubview:blurredPowerDown];
//Code to get blur depending of what is behind the view
blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
blurEffectView = [[UIVisualEffectView alloc]initWithEffect:blur];
blurEffectView.frame = blurredPowerDown.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[blurEffectView setAlpha:0.0];
[blurredPowerDown addSubview:blurEffectView];
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[blurEffectView setAlpha:1.0];
}
completion:^(BOOL finished) {
}];
//This is the view I use to center all the button that I will add (in red on the following picture)
centerView = [[UIView alloc] init];
[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];
centerView.backgroundColor = [UIColor redColor];
[blurEffectView addSubview:centerView];
[blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:blurEffectView
attribute:NSLayoutAttributeWidth
multiplier:0.7
constant:0]];
[blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:blurEffectView
attribute:NSLayoutAttributeHeight
multiplier:0.5
constant:0]];
[blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:blurEffectView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[blurEffectView addConstraint:[NSLayoutConstraint constraintWithItem:centerView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:blurEffectView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
//And this is the button that cause me issue
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton addTarget:self action:@selector(cancelView) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
cancelButton.frame = CGRectMake(0, 0, 80, 80);
cancelButton.clipsToBounds = YES;
cancelButton.layer.cornerRadius = 80/2.0f;
cancelButton.layer.borderColor=[UIColor whiteColor].CGColor;
cancelButton.layer.borderWidth=2.0f;
[centerView addSubview:cancelButton];
//Constraints removed as per UditS suggestion
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
这是它的功能截图
我真的不明白为什么我的UIButton始终保持在左上角(就好像它没有放置约束但我添加了它们!)
我真的很绝望!
在此先感谢您的帮助
编辑:如果我设置cancelButton.center = centerView.center; 这是我得到的:
EDIT2:正如所建议的那样,我设置了cancelButton.translatesAutoresizingMaskIntoConstraints = NO; 但结果如下:
编辑3:我们提前^^感谢@UditS建议(删除高度和宽度限制)按钮居中,但现在边框很奇怪
您应该禁用转换autoresizingMask到您的按钮的约束:
cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
附加:如果要将按钮的宽度和高度设置为80px,则应在这些上替换cancelButton的2个第一个约束:
[cancelButton addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:80]];
[cancelButton addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:80]];
问题可能在于你的CenterY约束,就像在你的代码中你正在使cancelButton
的CenterY等于centerView
的CenterX。
所以代替
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
你应该有
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
根据设置后的编辑
cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
按钮将占据centerView
全宽和高度,因为您通过以下约束进行设置
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0]];
[centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:centerView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0]];
删除这些约束并添加对称按钮的恒定高度和宽度。
根据编辑3 ,尝试向按钮添加宽度和高度约束。
[cancelButton addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:80.0]];
[cancelButton addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:80.0]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.