所以我试图创建一个模拟条形的自定义类,当点击时,展开以显示额外信息,然后再次点击,折叠并隐藏它。我还放入代码,以便它可以接受自己的子视图。一切正常,除了按钮不适用于子视图。知道为什么吗?以下是我的代码。
初始代码
- (void)initHeader {
UIButton *animateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[animateButton setFrame:CGRectMake(20, 20, 150, 20)];
[animateButton addTarget:self action:@selector(animateView:) forControlEvents:UIControlEventTouchUpInside];
[animateButton setTitle:@"ANIMATE LA" forState:UIControlStateNormal];
self.clipsToBounds = NO;
clippedView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 280, 0)];
clippedView.clipsToBounds = YES;
UILabel *label = [[UILabel alloc] init];
[label setText:@"EXTRA INFO"];
[label setFrame:CGRectMake(20, 150, 150, 20)];
[clippedView setBackgroundColor:[UIColor whiteColor]];
[clippedView addSubview:label];
[self addSubview:animateButton];
[self addSubview:clippedView];
}
// The code to add children
- (void)addChildComboView {
if (childView == nil) {
CGRect frame = CGRectMake(0, 100, baseWidth, 100);
childView = [[LAComboInfoView alloc] initWithFrame:frame];
[self initChildVars];
[self addSubview:childView];
}
else {
[childView addChildComboView];
}
}
// The code that runs when the button is pressed. All the frames are preset in another method which I will not post because it's basically just initialization.
- (IBAction)animateView:(id)sender {
/*
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.myButton.frame=SET_YOUR_FRAME;
} completion:^(BOOL finished)];
*/
if (x == 0) {
[self setFrame:expandedFrame];
[childView setFrame:expandedChildFrame];
[clippedView setFrame:expandedClippedViewFrame];
x = 1;
NSLog(@"Expanded");
}
else {
[self setFrame:collapsedFrame];
[childView setFrame:collapsedChildFrame];
[clippedView setFrame:collapsedClippedViewFrame];
x = 0;
NSLog(@"Collapsed");
}
}
// What's called on the code
LAComboInfoView *champOneView = [[LAComboInfoView alloc] initWithFrame:frame];
[champOneView setBackgroundColor:physicalColor];
[champOneView addChildComboView];
[_scroller addSubview:champOneView];
以下是两张图片,第一张是启动外观,第二张是按下按钮后。
除了childView按钮(白色方块)不起作用外,工作正常。
tl; dr:代码中的某些内容会导致儿童观看按钮无法正常工作。