iOS 11上的UIButton tap区域变小了

时间:2017-11-09 12:34:22

标签: objective-c uibutton ios11 xcode9 uiedgeinsets

我注意到我的UIButton的点击区域因iOS 11而变小。为了确认,我在其子视图上注释了所需的背景颜色,并在按钮上添加了紫色。

iOS 10.0.3(根据需要显示按钮) enter image description here

iOS 11.1(点按区域变小) enter image description here

我的代码如下。

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区域? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

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

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

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