我有一個自定義的UIView XIB
,它被加載到我的ViewController
上,並且在添加到子視圖之前將框架調整爲摺疊狀態。綁定到我的XIB
的UIButton顯示框架小於按鈕位置。當框架展開/摺疊時,如何隱藏和顯示我的UIButton?我的XIB沒有使用AutoLayout
。UIView(xib)框架之外的UIButton
ViewController.m
self.greenView = [[[NSBundle mainBundle] loadNibNamed:@"Green" owner:self options:nil] objectAtIndex:0];
[self.navigationController.view addSubview:self.greenView];
GreenView.h
@interface GreenView : UIView
@property (weak, nonatomic) IBOutlet UIView *expandView;
@property (nonatomic, getter = isExpanded) BOOL expand;
@property (weak, nonatomic) IBOutlet UIButton *testButton;
- (IBAction)testButtonTapped:(id)sender;
@end
GreenView.m
@interface GreenView()
@property (nonatomic) UITapGestureRecognizer *tapGesture;
@end
@implementation GreenView
- (void)awakeFromNib {
CGRect frame = self.frame;
frame.size.height = self.expandView.frame.size.height;
frame.origin.y = 200;
self.frame = frame;
self.expand = NO;
[self.expandView addGestureRecognizer:self.tapGesture];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (UITapGestureRecognizer *)tapGesture {
if (!_tapGesture) {
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClicked:)];
}
return _tapGesture;
}
- (void)tapClicked:(UIGestureRecognizer *)recognizer {
CGRect frame = self.frame;
self.expand = !self.expand;
frame.size.height = self.expand ? gvExpandedHeight : gvCollapsedHeight;
[UIView animateWithDuration:0.5 animations:^{
self.frame = frame;
} completion:^(BOOL finished) {
NSLog(@"Frame after animation: %@", NSStringFromCGRect(self.frame));
}];
}
- (IBAction)testButtonTapped:(id)sender {
NSLog(@"Test button tapped");
}
@end
已摺疊:
擴展:
GreenView.xib:
GrenenView.xib Struts和泉:
我沒有認出任何問題。請更新。 –