【问题标题】:UIButton in a custom UIView is not being triggered when tapped upon点击时不会触发自定义 UIView 中的 UIButton
【发布时间】:2021-12-29 01:33:27
【问题描述】:

我已经在自定义 UIView CustomButtonView 中放置了一个 UIButton。 UIButton 没有响应点击/单击事件,并且没有触发其相应的选择器方法。下面是我的代码;我做错了什么?

同样,自定义视图的背景颜色为红色。当我将自定义视图添加到视图控制器的视图中时,会显示红色矩形。但是,当我将 UIButton 添加到自定义视图(背景颜色为红色)时,仅显示 UIButton,而不显示红色矩形。为什么会这样?

//
//  CustomButtonView.h
//
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface  CustomButtonView : UIView

@property (strong, nonatomic) UIImageView *buttonImageView;
@property (strong, nonatomic) UIButton *button;

@end

NS_ASSUME_NONNULL_END

//
//  CustomButtonView.m
//

#import "CustomButtonView.h"

@implementation CustomButtonView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = [UIColor redColor];
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.backgroundColor = [UIColor greenColor];
        [_button setTitle:@"john doe" forState:UIControlStateNormal];
        [_button setTitle:@"john doe" forState:UIControlStateHighlighted];
        _button.translatesAutoresizingMaskIntoConstraints = NO;
        _button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
        _button.titleLabel.textColor = [UIColor whiteColor];
        self.userInteractionEnabled = NO;
        self.button.userInteractionEnabled = YES;
        //[self addSubview:_button];
        //[_button.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
        //[_button.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
    }
    return self;

}

@end
//
//  ViewController.m
//  TestApp
//
//

#import "ViewController.h"
#import "CustomButtonView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    _customButtonView = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _customButtonView.backgroundColor = [UIColor redColor];
    _customButtonView.translatesAutoresizingMaskIntoConstraints = NO;
    [_customButtonView.button addTarget:self action:@selector(customButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_customButtonView];
}

- (void)customButtonTapped:(id)sender{
    NSLog(@"Button tapped!");
}

@end

下面是一些屏幕截图。请点击链接。

When I display the UIButton inside the custom-view, only the button displays. The red rectangle of the custom-view doesn't display.

When I simply add the custom-view to my view-controller (ie with no UIButton added to the custom view), the red rectangle of the custom-view displays.

【问题讨论】:

    标签: ios objective-c uiview uibutton


    【解决方案1】:

    当您禁用特定视图的用户交互时,此用户交互不会转发到其子视图。或者换句话说;要使用户交互起作用,您需要确保链中的所有超级视图(超级视图的超级视图)都启用了用户交互。

    在您的情况下,您有self.userInteractionEnabled = NO;,这意味着self(包括您的按钮)的任何子视图都不会接受任何用户交互。

    在此问题旁边,仍有可能存在其他问题。例如:子视图将仅在其所有父视图的物理区域内接受用户交互。这意味着如果一个带有框架{ 0, 0, 100, 100 } 的按钮被放置在大小为{ 100, 50 } 的superview 上,那么只有按钮的顶部是可点击的。

    至于尺寸变化,这是一个完全不同的故事...... 您决定通过编写_customButtonView.translatesAutoresizingMaskIntoConstraints = NO; 在自定义视图上禁用自动调整大小掩码的翻译。这意味着您不会将frame 与其他约束一起使用。这意味着无论您设置什么框架(在您的情况下为 CGRectMake(0, 0, 100, 100)),此框架都将被任何其他可能影响您的自定义视图框架的内容覆盖。

    如果您的按钮代码被注释掉,则不会出现“可能影响自定义视图的框架”的情况,并且视图的大小保持为{ 100, 100 }。但是,一旦您添加了一个按钮,您就添加了额外的约束,这很可能导致您的按钮大小调整为 sizeToFit 以及它的超级视图,即您的自定义视图。

    为避免这种情况,您需要删除禁用自动调整掩码转换为自定义视图的约束。或者您需要通过向其添加约束来定义自定义视图大小,以便将它们设置为{ 100, 100 }。您可以简单地将heightAnchorwidthAnchor 约束为100

    【讨论】:

    • 感谢 Matic!你的两个建议都奏效了。将自定义视图上的 translatesAutoresizingMaskIntoConstraints 掩码设置为 YES,使按钮能够正确显示。同样,在 superview 上启用用户交互也有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多