我有一个自定义UIView XIB
加载到我的ViewController
,并且框架会在添加到子视图之前进行调整以折叠。绑定到XIB
的UIButton在帧小于按钮位置时显示。当框架正在扩展/折叠时,如何隐藏和显示我的UIButton?我的XIB没有使用AutoLayout
。
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和Springs:
答案 0 :(得分:1)
如果我理解你只想剪辑它,如果按钮在视图范围之外。在-awakeFromNib
添加:
self.clipsToBounds = YES;
使用此属性,您将告诉视图剪切不在其边界内的所有内容,即使视图位于外部,也可以查看绘制子视图。如果你经常使用它或者在重型动画中使用它,那么通过性能会有点贵 一种方法可以是在视图崩溃时隐藏它。
答案 1 :(得分:0)
您将框架更改为frame.origin.y = 200;
我认为你必须在你的nib文件中设置相对于UIButton
的{{1}}约束