Domanda

I have a custom UIView XIB that gets loaded onto my ViewController and the frame is adjusted to be collapsed before added to subview. The UIButton tied to my XIB is showing when the frame is smaller than the button location. How can I hide and show my UIButton as the frame is expanding/collapsing? My XIB is not using 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

Collapsed:

Collapsed

Expanded:

Expanded

GreenView.xib:

GreenView.xib

GrenenView.xib Struts and Springs:

Struts and springs of greenview.xib

È stato utile?

Soluzione

If I understood you just want to clip it, if the button is outside to the view bounds. In -awakeFromNib add:

self.clipsToBounds = YES;

Using this property you are telling the view to cut everything that is not inside its bounds, views caw draw subviews even if they are place outside. Is a little expensive by means of performance, if you use it a lot or during heavy animations.
One way around could be hide it while the view is collapsed.

Altri suggerimenti

You change the frame to frame.origin.y = 200;

I think you have to set the UIButton constraint relative to your GreenView in you nib file

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top