[英]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.