我在自定义的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下面是一些截图。请点击链接。
发布于 2021-11-18 09:17:56
当您禁用特定视图的用户交互时,此用户交互不会转发到其子视图。或者换句话说,为了让用户交互工作,您需要确保链中的所有superview (superview的superview或superview的superview)都启用了用户交互。
在您的示例中,您有self.userInteractionEnabled = NO;,这意味着self的任何子视图(包括您的按钮)将不接受任何用户交互。
在这个问题旁边,仍然有可能存在其他问题。例如:一个子视图将只接受其所有超级视图的物理区域内的用户交互。这意味着如果一个带有frame { 0, 0, 100, 100 }的按钮被放在一个大小为{ 100, 50 }的superview上,那么只有按钮的顶部是可点击的。
至于大小的变化,这是一个完全不同的故事。您决定通过编写_customButtonView.translatesAutoresizingMaskIntoConstraints = NO;在自定义视图上禁用自动调整蒙版大小的转换。这意味着您不会将frame与其他约束一起使用。这意味着无论您设置什么框架(在本例中为CGRectMake(0, 0, 100, 100)),此框架都将被任何其他可能影响您的自定义视图框架的内容覆盖。
在你的按钮代码被注释掉的情况下,没有“可能影响你的自定义视图框架”的东西,视图仍然是{ 100, 100 }的大小。但是一旦你添加了一个按钮,你就添加了额外的约束,这很可能导致将你的按钮调整到sizeToFit以及它的superview,这是你的自定义视图。
要避免这种情况,您需要取消禁用自动调整蒙版大小转换为自定义视图上的约束。或者,您需要通过添加约束来定义自定义视图大小,以便将其设置为{ 100, 100 }。您可以简单地将heightAnchor和widthAnchor限制为100。
https://stackoverflow.com/questions/70015137
复制相似问题