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.
}


0 0
原创粉丝点击