【问题标题】:UIButton tap area got smaller on iOS 11iOS 11 上的 UIButton 点击​​区域变小了
【发布时间】:2018-04-22 10:56:27
【问题描述】:

我注意到我的 UIButton 的点击区域自 iOS 11 以来变小了。为了确认,我在其子视图上注释掉了所需的背景颜色,并将紫色放在按钮本身上。

iOS 10.0.3(按需要显示按钮)

iOS 11.1(点击区域变小)

我的代码如下。

UIImage *ringImage = [UIImage imageNamed:@"ball20r"];
UIEdgeInsets insets = UIEdgeInsetsMake(10,10,10,10); //padding
UIImage *stretchableImage = [ringImage resizableImageWithCapInsets:insets];

UIImageView *imageView   = [[UIImageView alloc]initWithImage:stretchableImage];
// imageView.backgroundColor = backgroundColor; //disabled for testing
imageView.frame = CGRectMake(0, 0, label.frame.size.width + 16.0, label.frame.size.height + 16.0);
[imageView addSubview:label];


label.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = @{@"label":label};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(8)-[label]-(8)-|"
                                                                options:0
                                                                metrics:nil
                                                                  views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(8)-[label]-(8)-|"
                                                                options:0
                                                                metrics:nil
                                                                  views:viewsDictionary];
[imageView addConstraints:constraint_H];
[imageView addConstraints:constraint_V];


UIButton *calendarButton = [UIButton buttonWithType:UIButtonTypeCustom];
[calendarButton addTarget:self action:@selector(chooseACalendarToSave) forControlEvents:UIControlEventTouchUpInside];
calendarButton.frame = imageView.frame;
[calendarButton addSubview:imageView];

//added for testing only
calendarButton.backgroundColor = [UIColor purpleColor];

_calendarButton = [[UIBarButtonItem alloc]initWithCustomView:calendarButton];

如何让 iOS 区域像 iOS 10.0.3 及更早版本一样? 感谢您的帮助。

【问题讨论】:

    标签: objective-c uibutton ios11 xcode9 uiedgeinsets


    【解决方案1】:

    答案就在这里。 iOS 11 UIBarButtonItem images not sizing

     if (@available(iOS 9, *)) {
          [cButton.widthAnchor constraintEqualToConstant: standardButtonSize.width].active = YES;
          [cButton.heightAnchor constraintEqualToConstant: standardButtonSize.height].active = YES;
     }
    

    只要我插入上面的代码,按钮就开始按预期工作。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多