UIVIew 中的 UIButton 不在 iPhone 应用程序中居中

Posted

技术标签:

【中文标题】UIVIew 中的 UIButton 不在 iPhone 应用程序中居中【英文标题】:UIButton inside UIVIew not centered in iPhone app 【发布时间】: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 不是,与屏幕截图中的大小相同 【参考方案1】:

您应该为您的按钮禁用翻译 autoresizingMask 到约束:

cancelButton.translatesAutoresizingMaskIntoConstraints = NO;

补充:如果你想将按钮的宽度和高度设置为 80px,你应该在这些上替换取消按钮的 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]];

【讨论】:

感谢您的建议,但请参阅有问题的我的 EDIT2,它现在需要整个子视图。我希望它与第一个屏幕截图中的相同 好的。现在布局是正确的。您写道,按钮的宽度和高度应该等于 centerView 的宽度和高度。怎么了?可能你想设置另一个宽度? 是的,只有 80 px,我正在尝试您的代码,并会在尝试后立即更新情况! 我的设备在属性后崩溃:NSLayoutAttributeNotAnAttribute 和常量:80 :/ 你删除了这个和下一个吗? [centerView addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton 属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:centerView 属性:NSLayoutAttributeWidth 乘数:1.0 常量:0]];【参考方案2】:

问题可能出在您的 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]];

删除这些约束并为按钮的恒定高度和宽度添加约束。

按照Edit 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]];

【讨论】:

复制/粘贴错误,在我的代码中是正确的。我将编辑问题 按照@Dmitry 的建议尝试cancelButton.translatesAutoresizingMaskIntoConstraints = NO; 无法按我的意愿工作,因为按钮现在占据了整个子视图:/ 哦,我忘记了这些限制...我们前进^^见我的编辑3。现在按钮变成了一个奇怪的边框 我的设备在属性后崩溃:NSLayoutAttributeNotAnAttribute 和常量:80 :/

以上是关于UIVIew 中的 UIButton 不在 iPhone 应用程序中居中的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 UIButton 不在我认为应该在的位置?

UIView中的UIButton不可点击

UIView动画中的UIButton字体大小问题

UITableViewCell 与 UIView 中的 UIButton 性能

自定义 UIView 类中的 UIButton 不起作用

尽管 userInteractionEnabled = true,但 UIView 中的 UIButton 没有响应触摸事件