【发布时间】:2016-01-28 15:07:50
【问题描述】:
我实际上以编程方式设置了一个自定义 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;但结果如下:
EDIT3:我们提前^^感谢@UditS的建议(删除高度和宽度约束)按钮居中但现在边框很奇怪
【问题讨论】:
-
对于放置在中心只写 yourButton.center = yourView.center
-
您的
cancelButton是否需要与centerView具有相同的高度和宽度? -
@Gagan_iOS 我已经根据你的建议编辑了我的问题。
-
@UditS 不是,与屏幕截图中的大小相同
标签: ios objective-c iphone uiview uibutton