我有一个包含滚动视图的视图控制器。在滚动视图的内部,我有一个带有自己的自定义NIB的自定义标题视图。我遇到的问题是自定义视图内的按钮上的触摸事件未被触发。通过阅读其他答案,我尝试将自定义视图的clipsToBounds
属性设置为YES,这样做会导致标题视图没有可见内容。这可能是问题的一部分,但我不知道如何解决它。我的代码如下:
在视图控制器中:
@interface MyViewController ()
@property (weak, nonatomic) DetailHeaderView *headerView; // The custom view placed inside contentView
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *contentView; // content view for scrollView
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Load the Header View
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"DetailHeaderView" owner:nil options:nil];
self.headerView = [views firstObject];
self.headerView.psychic = self.psychic;
// Add the header view to the scroll view
[self.contentView addSubview:self.headerView];
[self setConstraints];
}
-(void)setConstraints {
[self.view removeConstraints:self.view.constraints];
[self.scrollView removeConstraints:self.scrollView.constraints];
[self.contentView removeConstraints:self.contentView.constraints];
[self.headerView removeConstraints:self.headerView.constraints];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.top.equalTo(self.view);
make.bottom.equalTo(self.view);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView);
make.width.equalTo(self.scrollView);
}];
[self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView);
make.left.equalTo(self.contentView);
make.width.equalTo(self.contentView);
make.height.equalTo(@120);
}];
}
在我的自定义视图文件中:
@interface DetailHeaderView()
@property (weak, nonatomic) IBOutlet UIPhotoView *photoView;
@property (weak, nonatomic) IBOutlet UILabel *levelLabel;
@property (weak, nonatomic) IBOutlet UILabel *experienceLabel;
@property (weak, nonatomic) IBOutlet UIView *horizontalRuleView;
@property (weak, nonatomic) IBOutlet StarRatingView *ratingView;
@property BOOL constraintsAlreadySet;
@end
@implementation DetailHeaderView
-(void)updateConstraints {
[super updateConstraints];
// self.clipsToBounds = YES; <- When I uncomment this, I see nothing.
[self removeConstraints:self.constraints];
[self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).with.offset(20);
make.left.equalTo(self).with.offset(8);
make.width.equalTo(@120);
make.height.equalTo(@100);
}];
[self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.photoView).with.offset(8);
make.left.equalTo(self.photoView.mas_right).with.offset(8);
}];
// self.priceButton below is the button that is not clickable.
// It is not set as a property in this .m file because it is declared as a
// public property in the .h so that the parent view can respond to events.
[self.priceButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.levelLabel);
make.right.equalTo(self).with.offset(-8);
}];
[self.horizontalRuleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.levelLabel);
make.top.equalTo(self.levelLabel.mas_bottom).with.offset(8);
make.right.equalTo(self.priceButton);
make.height.equalTo(@1);
}];
[self.experienceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.horizontalRuleView.mas_bottom).with.offset(8);
make.left.equalTo(self.horizontalRuleView);
}];
[self.ratingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.experienceLabel.mas_bottom).with.offset(8);
make.left.equalTo(self.experienceLabel);
}];
}
同样,如果未在“DetailHeaderView”中设置clipsToBounds
,我会看到所有内容,但self.priceButton
属性没有发送触摸事件。
任何帮助将不胜感激!
答案 0 :(得分:0)
视图层次结构中的某些内容会阻止触摸或吞噬它们。我的猜测是你需要在contentView和/或headerView上设置userInteractionEnabled = YES。