iOS弹出底部视图简单实现
- 项目基本目录
其中xib文件用来自定义需要弹出的视图。
在主控制器里设置popview的frame等信息代码如下:
底部视图(popview)初始化放在父类视图的最顶部或者说是整个屏幕的最底部,宽高自定义
- (void)viewDidLoad { [super viewDidLoad]; _popview = [[popview alloc]init]; _popview.frame = CGRectMake(0, self.view.frame.size.height, 400, 700); [self.view addSubview:_popview]; }
当点击相应按钮的时候让底部视图往上移
- (IBAction)pop:(id)sender { [ UIView animateWithDuration:0.3 animations:^{ CGAffineTransform tf = CGAffineTransformMakeTranslation(0, -_popview.frame.size.height); [_popview setTransform:tf]; }]; }
popview视图中的代码如下:
@interface popview () @property (weak, nonatomic) IBOutlet UIView *bkview; @end @implementation popview - (instancetype)init{ //从xib中初始化视图 if (self = [super init]) { NSArray *binview = [[NSBundle mainBundle] loadNibNamed:@"popview" owner:nil options:nil]; self = [binview lastObject]; } return self; } //点击取消按钮的时候移除并且让弹出视图的背景视图隐藏,等到底部视图彻底移到底部的时候在设置显示背景视图(背景视图主要用来模糊效果) - (IBAction)cancel:(id)sender { [_bkview setHidden:YES]; [UIView animateWithDuration:0.3 animations:^{ self.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { [_bkview setHidden:NO]; }]; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步