gpt4 book ai didi

ios - 使用自动布局 (iOS) 时,UIView 中的 UIButton 不响应用户点击

转载 作者:行者123 更新时间:2023-11-28 22:00:57 27 4
gpt4 key购买 nike

我正在尝试创建一个类似网格的菜单,如果点击 rightBarButton 则可以显示该菜单。看起来布局是正确的(见下图),但单元格(每个单元格都是一个 UIButton)不响应用户点击。

screenshot of the dropdown

下拉列表(标有红框的部分)由两个 View 组成。一个是 GridCell,另一个是 GridCollection。 GridCell 是一个简单的 View ,其中包含一个占据整个容器的 UIButton。 GridCollection 初始化 GridCells 并按顺序放置它们。

下面是从GridCell中提取出来的代码

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}

- (id)initWithColor:(UIColor *)color image:(UIImage *) cellImage title:(NSString *) title {
self = [self init];
if(self) {
self.backgroundColor = color;
self.image = cellImage;
self.title = title;
if(title == nil) self.title = @"default";
}
return self;
}

- (void)updateConstraints {
[super updateConstraints];
[self setTranslatesAutoresizingMaskIntoConstraints:NO];

//button should fill container
UIButton *button = [[[UIButton alloc] init] autorelease];
self.cellButton = button;
[self.cellButton addTarget:self action:@selector(cellTapped) forControlEvents:UIControlEventTouchUpInside];
[self.cellButton setTitle:self.title forState:UIControlStateNormal];
[self.cellButton setTitle:@"pressed" forState:UIControlStateHighlighted];
[self.cellButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.cellButton setBackgroundImage:self.image forState:UIControlStateNormal];
[self.cellButton setUserInteractionEnabled:YES];
[self.cellButton setTranslatesAutoresizingMaskIntoConstraints:NO];

[self addSubview:self.cellButton];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_cellButton]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_cellButton)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_cellButton]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_cellButton)]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:60]];
}

- (void)cellTapped {
NSLog(@"cell %d pressed ", self.tag);
}

- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"Printing button's frame: %@", NSStringFromCGRect(self.cellButton.frame));
}

下面是从GridCollection中提取出来的代码

- (void)updateConstraints {
[super updateConstraints];
GridCell *previousCell = nil;
GridCell *firstCell = nil;
for (int i = 1; i <= self.numberOfCells; ++i) {
GridCell *cell = [[[GridCell alloc] initWithColor:i % 2 == 0 ? [UIColor brownColor] : [UIColor grayColor] image:nil title:[NSString stringWithFormat:@"Cell %d", i]] autorelease];
cell.tag = i;
[self addSubview:cell];
[cell setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraint:[NSLayoutConstraint constraintWithItem:cell attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:self.superview.frame.size.width/4]];
if (!previousCell) { //pin to top left
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[cell]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(cell)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[cell]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(cell)]];
firstCell = cell;
} else { //pin to previous
if(i % 4 == 1 && i != 1) {
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstCell][cell]" options:0 metrics:nil views:@{@"cell" : cell, @"firstCell" : firstCell}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[cell]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(cell)]];
} else {
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousCell]-0-[cell]" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(previousCell, cell)]];
}
}
previousCell = cell;
}
[self layoutIfNeeded];
}

出于调试目的,我将 NSLog(@"Printing button's frame: %@", NSStringFromCGRect(self.cellButton.frame)) 放在 GridCell 的 layoutSubview 方法中。我发现它们都返回 0,0 作为按钮的来源。我想这也许就是我的单元格中的 UIButton 没有响应的原因,对吗?

2014-08-04 21:36:42.837 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.838 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.839 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.840 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.840 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.841 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.842 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}
2014-08-04 21:36:42.843 projectName[38661:60b] Printing button's frame: {{0, 0}, {80, 60}}

我想知道在这种情况下如何正确定位单元格的原点。

提前致谢。

最佳答案

这可能是由以下问题之一引起的:

  1. 按钮托管在其父 View 的边界之外。这也意味着如果父 View 有边界 {0,0}
  2. 您正在从 xib 加载单元格,但这些单元格的 Root View 不是 UICollectionViewCell

关于ios - 使用自动布局 (iOS) 时,UIView 中的 UIButton 不响应用户点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25120421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com