AutoLayout test

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *str = @"";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    UITextView *t0 = [[UITextView alloc]initWithFrame:CGRectZero];
    t0.font = [UIFont fontWithName:@"AvenirNext-Regular" size:14];
    t0.backgroundColor = RGBA(230,230,230,1.0);
    t0.delegate = self;
    t0.text = str;
    t0.scrollEnabled = NO;
    t0.layoutManager.allowsNonContiguousLayout = NO;
    t0.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:t0];
    NSArray *tvw =
    [NSLayoutConstraint constraintsWithVisualFormat:@"[t(300)]"
                                            options:0
                                            metrics:nil
                                              views:@{@"t":t0}];
    [self.view addConstraints:tvw];
    [self.view addConstraint: [NSLayoutConstraint constraintWithItem:t0  attribute:NSLayoutAttributeCenterX  relatedBy:NSLayoutRelationEqual  toItem:self.view  attribute:NSLayoutAttributeCenterX  multiplier:1  constant:0 ] ] ;
    
    t0.text = @"A";
    CGSize size = [t0 sizeThatFits:CGSizeMake(300,100000)];
    tvh =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[t(height)]"
                                            options:0
                                            metrics:@{@"height":[NSNumber numberWithFloat:size.height]}
                                              views:@{@"t":t0}];
    [self.view addConstraints:tvh];
    t0.text = @"";
    

    
    view = [[UIView alloc]initWithFrame:CGRectZero];
    view.translatesAutoresizingMaskIntoConstraints = NO;
    view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view];
    
    
    NSDictionary *metricsDictionary = @{@"height":@"50",@"bottom":[NSNumber numberWithFloat:-50.0]};
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
    widthCont =
    [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[view]-0-|"
                                            options:0
                                            metrics:metricsDictionary
                                              views:viewsDictionary];
    [self.view addConstraints:widthCont];
    
    heightCont =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(height)]-bottom-|"
                                            options:0
                                            metrics:metricsDictionary
                                              views:viewsDictionary];
    [self.view addConstraints:heightCont];
}

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


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    return YES;
}


// キーボードが表示される時に呼び出されますー
- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect rect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSDictionary *metricsDictionary = @{@"height":@"50",@"bottom":[NSNumber numberWithFloat:rect.size.height]};
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
    
    [self.view removeConstraints:heightCont];
    heightCont =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(height)]-bottom-|"
                                            options:0
                                            metrics:metricsDictionary
                                              views:viewsDictionary];
    [self.view addConstraints:heightCont];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL comp){
    
    }];
}

// キーボードが非表示になる時に呼び出されます
- (void)keyboardWillHide:(NSNotification *)notification {
    NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSDictionary *metricsDictionary = @{@"height":@"50",@"bottom":[NSNumber numberWithFloat:-50.0]};
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
    
    [self.view removeConstraints:heightCont];
    heightCont =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(height)]-bottom-|"
                                            options:0
                                            metrics:metricsDictionary
                                              views:viewsDictionary];
    [self.view addConstraints:heightCont];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
}

-(void)textViewDidChange:(UITextView *)textView{
    CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width,100000)];
    [self.view removeConstraints:tvh];
    tvh =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[t(height)]"
                                            options:0
                                            metrics:@{@"height":[NSNumber numberWithFloat:size.height]}
                                              views:@{@"t":textView}];
    [self.view addConstraints:tvh];
}




@end