我實際上是以編程方式設置一個自定義的UIView,它將包含幾個UIButton,並且工作正常。 但我想要做的是將一個UIButton放在我已經以編程方式創建的子自定義UIView中。下面是我在實際使用UIVIew裏面的UIButton沒有集中在iPhone應用程序
//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'的? – UditS
@Gagan_iOS我用你的建議編輯了我的問題。 – Synny