【问题标题】:UIButton outside of UIView (xib) frameUIView (xib) 框架外的 UIButton
【发布时间】:2014-05-15 13:07:00
【问题描述】:

我有一个自定义的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 支柱和弹簧:

【问题讨论】:

  • 我没有发现任何问题。请更新。

标签: iphone objective-c uiview uibutton xib


【解决方案1】:

你把框架改成frame.origin.y = 200;

我认为您必须在您的 nib 文件中设置相对于您的 GreenViewUIButton 约束

【讨论】:

  • 更新了我的帖子。在我的 XIB 文件中添加了图像。我的 UIButton 与我的 XIB 相关联,因为我希望它在展开时显示,但在折叠时隐藏。
【解决方案2】:

如果我理解你只是想剪辑它,如果按钮在视图边界之外。在-awakeFromNib 添加:

self.clipsToBounds = YES;

使用这个属性,你告诉视图剪掉所有不在其边界内的东西,即使它们位于外部,视图也会绘制子视图。如果您经常使用它或在繁重的动画中使用它,则性能有点贵。
一种解决方法是在视图折叠时隐藏它。

【讨论】:

  • 谢谢!!!效果很好!我想在折叠时隐藏它,但找不到平滑过渡到显示它。更改 alpha 有效,但我不太喜欢它。这将是我的控制器上唯一需要clipsToBounds 的视图
猜你喜欢
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2011-11-02
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 2015-01-07
相关资源
最近更新 更多