2014-04-03 36 views
0

我有一個自定義的UIView XIB,它被加載到我的ViewController上,並且在添加到子視圖之前將框架調整爲摺疊狀態。綁定到我的XIB的UIButton顯示框架小於按鈕位置。當框架展開/摺疊時,如何隱藏和顯示我的UIButton?我的XIB沒有使用AutoLayoutUIView(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 

已摺疊:

Collapsed

擴展:

Expanded

GreenView.xib:

GreenView.xib

GrenenView.xib Struts和泉:

Struts and springs of greenview.xib

+0

我沒有認出任何問題。請更新。 –

回答

1

。在-awakeFromNib添加:

self.clipsToBounds = YES; 

使用這個屬性,你告訴視圖切一切,是不是它的邊界內,觀點啼平局子視圖,即使它們之外。如果您在演出過程中使用它或在繁重的動畫中使用它,性能方面會有點貴。
在視圖摺疊時,可以將其隱藏起來。

+0

謝謝!!!這工作完美!我想在隱藏它的同時崩潰,但無法順利過渡到展示它。改變阿爾法工作,但我不是很喜歡它。這將是我的控制器上需要'clipsToBounds'的唯一視圖 –

0

你改變幀frame.origin.y = 200;

我認爲你有相對UIButton約束。如果我明白你只是想將它夾,如果按鈕是外部視圖範圍設置爲您GreenView在你的筆尖文件

+0

更新了我的文章。添加了我的XIB文件的圖像。我的UIButton與我的XIB綁定,因爲我希望它在展開時顯示,但在展開時隱藏。 –