2016-01-28 72 views
1

我實際上是以編程方式設置一個自定義的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]]; 

這裏的代碼是什麼做

enter image description here

我真的不明白,爲什麼我的UIButton永遠留在左上角的屏幕截圖(如如果它沒有放置限制,但我已經添加了它們!)

我真的很絕望!

在此先感謝您的幫助

編輯:如果我設置cancelButton.center = centerView.center;這裏是我所得到的:

enter image description here

EDIT2:如所建議的,我給自己定cancelButton.translatesAutoresizingMaskIntoConstraints = NO;但這裏是結果:

enter image description here

EDIT3:我們提前^^感謝@UditS建議(刪除高度和寬度的限制)按鈕爲中心,但現在的邊界是很奇怪的

enter image description here

+0

爲中心將只寫yourButton.center = yourView.center –

+0

你需要你的'cancelButton'是相同的高度和寬度'centerView'的? – UditS

+0

@Gagan_iOS我用你的建議編輯了我的問題。 – Synny

回答

2

你應該禁用翻譯autoresizingMask爲您的按鈕限制:

cancelButton.translatesAutoresizingMaskIntoConstraints = NO; 

此外:如果您要設置寬度和按鈕的高度80px,你應該對這些替代2個第一約束cancelButton :

[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]]; 
+0

感謝您的建議,但請參閱我的EDIT2問題,現在需要整個子視圖。我喜歡它在第一屏截圖 – Synny

+0

好吧。現在佈局是正確的。你寫道,按鈕的寬度和高度應該等於centerView的寬度和高度。怎麼了?可能你想設置另一個寬度? –

+0

是的,只有80像素,我正在嘗試你的代碼,並會嘗試儘快更新情況! – Synny

1

這個問題可能與您的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,因爲你通過fol設置它降低限制

   [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]]; 
+0

的尺寸不一樣,複製/粘貼錯誤,在我的代碼中是正確的。我將編輯問題 – Synny

+0

嘗試'cancelButton.translatesAutoresizingMaskIntoConstraints = NO;'建議@Dmitry – UditS

+0

不工作,因爲我想按鈕現在需要整個子視圖:/ – Synny

相關問題