当我为自定义视图设置动画时,视图的高度会发生变化

时间:2017-05-16 03:07:03

标签: ios xib

我的BetRecordAniChooserView的xib:

enter image description here

模拟器中的我的ViewController:

您可以看到选择器视图高度的背景视图是reduce。

我的代码如下:

BetRecordAniChooserView.h:

#import <UIKit/UIKit.h>

typedef void(^ChooseBlock)(NSString *choosedStr);

@interface BetTRecordAniChooserView : UIView

@property (nonatomic, assign) UIViewController *owener;  
@property (nonatomic, assign) BOOL isShow;   

@property (nonatomic, copy) ChooseBlock block;   

- (void)showSelf;
- (void)hideSelf;

@end

BetRecordAniChooserView.m:

#import "BetTRecordAniChooserView.h"

@interface BetTRecordAniChooserView ()

@property (weak, nonatomic) IBOutlet UIButton *all_button;
@property (weak, nonatomic) IBOutlet UIButton *check_pending_button;
@property (weak, nonatomic) IBOutlet UIButton *deposited_button;
@property (weak, nonatomic) IBOutlet UIButton *have_cancel_button;


@end

@implementation BetTRecordAniChooserView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib {

    [super awakeFromNib];

    self.frame = CGRectMake(0, 0, self.bounds.size.width, 100);

    self.all_button.selected = YES;
}

#pragma mark - actions

- (IBAction)allAction:(UIButton *)sender {

    self.block(sender.titleLabel.text);
}

- (IBAction)checkPendingAction:(UIButton *)sender {
    self.block(sender.titleLabel.text);
}

- (IBAction)haveDepositeAction:(UIButton *)sender {
    self.block(sender.titleLabel.text);
}

- (IBAction)haveCancelAction:(UIButton *)sender {
    self.block(sender.titleLabel.text);
}



#pragma mark - methods

- (void)showSelf {

    CGRect temp_frame = self.frame;

    self.isShow = YES;

    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(temp_frame.origin.x, temp_frame.origin.y + temp_frame.size.height, temp_frame.size.width, temp_frame.size.height);
    }];

}

- (void)hideSelf {

    CGRect temp_frame = self.frame;

    self.isShow = NO;

    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(temp_frame.origin.x, temp_frame.origin.y - temp_frame.size.height, temp_frame.size.width, temp_frame.size.height);
    } completion:^(BOOL finished) {

    }];
}



@end

在我的ViewController.m中:

#import "ViewController.h"
#import "BetTRecordAniChooserView.h"

@interface ViewController ()
{
    BetTRecordAniChooserView *_chooser_view;  
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _chooser_view = [[NSBundle mainBundle] loadNibNamed:@"BetTRecordAniChooserView" owner:self options:nil].firstObject;
    //float width = self.view.bounds.size.width;
    //float height = 100.f;
    //_chooser_view.frame = CGRectMake(0, -height + 64, width, height);
    _chooser_view.owener = self;

    [self.view addSubview:_chooser_view];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)actionA:(UIButton *)sender {


    if (_chooser_view.isShow) {
        [_chooser_view hideSelf];
    } else {
        [_chooser_view showSelf];
    }
}

@end

您可以在BetRecordAniChooserView awakeFromnNib方法中看到:

我设置的框架高度100

self.frame = CGRectMake(0, 0, self.bounds.size.width, 100);

但是当我启动我的模拟器时,它变为36(按钮下方的灰色视图)。

  

(lldb)po self.frame
  (origin =(x = 0,y = 0),size =(width = 375,height = 36))

1 个答案:

答案 0 :(得分:0)

我找到了原因:

起初我使用灰色后视图的尾部,前导,底部,顶部到它的超视图,我得到了这个问题。

然后我删除了Bottom Space Constraint,并为其添加了高度约束。

enter image description here

然后我再没有问题,我也可以将高度约束拖到.m文件中,方便改变高度。

但是我不知道是否有一种方法我不使用我的设定高度约束方法,仍然使用尾随,前导,下限,顶部约束来获得需求效果。