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

Posted

技术标签:

【中文标题】当我为自定义视图设置动画时,视图的高度会发生变化【英文标题】:When I animate the custom view, the height of the view changes 【发布时间】:2017-05-16 03:07:03 【问题描述】:

我的 BetRecordAniChooserView 的 xib :

我在模拟器中的 ViewController:

可以看到选择器视图的背景视图高度降低了。

我的代码如下:

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

可以在BetRecordAniChooserViewawakeFromnNib方法中看到:

我设置的框架高度100:

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

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

(lldb) po self.frame (原点 = (x = 0, y = 0), 大小 = (宽度 = 375, 高度 = 36))

【问题讨论】:

【参考方案1】:

我找到了原因:

起初我使用灰色后视图的尾随、前导、底部、顶部到其父视图,我遇到了这个问题。

然后我删除底部空间约束,并为其添加高度约束。

那我就没有这个问题了,我也可以把高度约束拖到.m文件中,方便修改高度。

但是不知道有没有方法我不使用我设置的高度约束的方法,仍然使用尾部、前导、底部、顶部约束来获得需求效果。

【讨论】:

以上是关于当我为自定义视图设置动画时,视图的高度会发生变化的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 中将内容包装为自定义视图

将高度设置为自定义视图组

动画集合视图高度变化(Swift)

为自定义活动指示器视图使 UIView 半透明

动画 UIView 的子视图在动画期间不保持约束

当我推送一个新的视图控制器时,自动布局高度发生变化