addchildviewcontroller实现侧栏弹出和收回view处理
来源:互联网 发布:美德山东网络书画展 编辑:程序博客网 时间:2024/06/15 07:13
直接贴代码
#import "ViewController.h"
#import "PopViewController.h"
#import "ListViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIViewController *currentController;
@property (nonatomic, strong) PopViewController *popController;
@property (nonatomic, strong) ListViewController *listController;
@property (nonatomic, strong) NSLayoutConstraint *left;
@property (nonatomic, strong) NSLayoutConstraint *top;
@property (nonatomic, strong) NSLayoutConstraint *width;
@property (nonatomic, strong) NSLayoutConstraint *height;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_listController = [ListViewController new];
[_listController.view setFrame:self.view.frame];
UIButton *button = [[UIButton alloc] init];
[button addTarget:self action:@selector(showPop:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor whiteColor];
button.selected = NO;
[button setTitle:@"Pop" forState:UIControlStateNormal];
[_listController.view addSubview:button];
button.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *centerx = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_listController.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant: 1.0];
NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_listController.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant: 1.0];
NSArray *nextButtonContraintArray = [NSArray arrayWithObjects:centerx, centerY, nil];
[_listController.view addConstraints:nextButtonContraintArray];
_listController.view.backgroundColor = [UIColor blueColor];
[self addChildViewController:_listController];
[_listController didMoveToParentViewController:self];
[self.view addSubview:_listController.view]; //将list view controller的view作为了第一个需要展示的view
_popController = [PopViewController new];
_popController.view.backgroundColor = [UIColor redColor];
[self addChildViewController:_popController];
[_popController didMoveToParentViewController:self];
[self.view addSubview:_popController.view];
_popController.view.translatesAutoresizingMaskIntoConstraints = NO;
_left = [NSLayoutConstraint constraintWithItem:_popController.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant: -120.0f];
_top = [NSLayoutConstraint constraintWithItem:_popController.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant: -44.0f];
_width = [NSLayoutConstraint constraintWithItem:_popController.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant: 120.0f];
_height = [NSLayoutConstraint constraintWithItem:_popController.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant: self.view.frame.size.height + 44.0f];
NSArray *listViewConstraints = [NSArray arrayWithObjects:_left, _top, _width, _height, nil];
[self.view addConstraints:listViewConstraints]; // 这里使用了约束,为了更好的适应旋转屏幕的操作
}
- (void)showPop:(id)sender {
UIButton *button = (UIButton *)sender;
if (!button.isSelected) {
//selected
[UIView animateWithDuration:0.3f delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
_left.constant = 0.0f ; // 使用这种方法来修改约束即可
[self.view layoutIfNeeded]; //必须调用这个方法,否则动画没有效果
} completion:^(BOOL finished) {
}];
button.selected = YES;
} else {
//not select
[UIView animateWithDuration:0.3f delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
_left.constant = -120.0f ;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
button.selected = NO;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "PopViewController.h"
@interface PopViewController ()
@end
@implementation PopViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [[UIButton alloc]init];
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"设置" forState:UIControlStateNormal];
[self.view addSubview:button];
button.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *centerx = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant: 1.0];
NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant: 1.0];
NSArray *nextButtonContraintArray = [NSArray arrayWithObjects:centerx, centerY, nil];
[self.view addConstraints:nextButtonContraintArray];
[self.view layoutIfNeeded];
NSLog(@"%f,%f,%f,%f", button.frame.origin.x, button.frame.origin.y, button.frame.size.width, button.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- addchildviewcontroller实现侧栏弹出和收回view处理
- 随软键盘弹出和收回view(二)
- 【ios】AddChildViewController 实现 Pop View
- android按钮弹出和收回
- React Native 处理TextInput在输入后,点击布局空白处软件盘能弹出和收回。
- iOS: 通过addChildViewController实现 Segment 下的 View 的切换
- iOS: 通过addChildViewController实现 Segment 下的 View 的切换
- iOS: 通过addChildViewController实现 Segment 下的 View 的切换
- jq实现购物网站顶部广告自动弹出收回效果
- 根据虚拟键盘弹出和收回控制UITextfield位置
- 自定义View实现弹出效果和文字居中
- 如何实现监听Android输入法弹出收回,为什么要使用接口
- addChildViewController 实现网易新闻标签栏效果的demo
- android 实现发散弹出view
- Android防止键盘弹出后顶起布局和弹窗消失后输入法没有收回
- 仿QQ空间评论随软键盘弹出和收回一个输入布局
- addChildViewController
- addChildViewController
- 移动端开发App、Web屏幕适配参考
- 设计模式----代理模式
- Java内存模型与线程
- 144. Binary Tree Preorder Traversal Stack version
- 浅析深究什么是中间件
- addchildviewcontroller实现侧栏弹出和收回view处理
- 使用Glide轻松播放网络GIF图片
- 动多态和静多态
- 使用010Editor模板查看ELF、DEX等文件结构
- MyBatis整合到Spring 配置文件总结与分析
- 研发质量管理工作经验总结(一)----质量管理知识
- 浅谈Canvas的状态绘图
- php 获取时间,时间戳(上周,上月,上半年,上一年,自定义时间),strtotime(),date()
- 车牌处理程序-学-使-警