【问题标题】:Can't receive touch events in custom UIView with XIB无法使用 XIB 在自定义 UIView 中接收触摸事件
【发布时间】:2015-01-10 01:16:22
【问题描述】:

我有一个包含滚动视图的视图控制器。在滚动视图内部,我有一个带有自己的自定义 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属性没有发送触摸事件。

任何帮助将不胜感激!

【问题讨论】:

    标签: objective-c uiview autolayout xib masonry


    【解决方案1】:

    您的视图层次结构中的某些东西正在阻止触摸或吞噬它们。我猜你需要在 contentView 和/或 headerView 上设置 userInteractionEnabled=YES。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      相关资源
      最近更新 更多