1

我注意到我的 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 10.0.3 及更早版本一样为 iOS 创建区域?谢谢你的帮助。

4

1 回答 1

0

答案就在这里。 iOS 11 UIBarButtonItem 图像没有调整大小

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

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

于 2017-11-09T18:31:40.440 回答