iOS Switching an embedded view in storyboard

后端 未结 3 1003
予麋鹿
予麋鹿 2021-02-04 18:16

I have been trying to figure this out all day and I know it should be able to be done, but being new to iOS development using Objective-C and not Appcelerator I\'m having newbie

3条回答
  •  不思量自难忘°
    2021-02-04 18:43

    You can switch the view controllers like you would do with a container view controller. I made a project where I added a container view (2 actually, but we're only dealing with one here), and added this code to the embedded controller that you get automatically when you drag in the container view. The controller I'm switching to, NewRightController, is a UIViewController subclass -- in the storyboard I set the controller's size to "Freeform", and changed the size of the view to match the size of the embedded controller's view. This doesn't actually affect the size of the view (it still logs as full screen), it just makes it easier to layout the subviews.

    -(IBAction)switchToNewRight:(id)sender {
        UIView *rcView = [(ViewController *)self.parentViewController rightView]; // rcView is the right container view in my root view controller that self is embedded in.
        NewRightController *newRight = [self.storyboard instantiateViewControllerWithIdentifier:@"NewRight"];
        newRight.oldRightController = self; // pass self to new controller so I can come back to the same instance
        newRight.view.frame = rcView.bounds;
        [self.parentViewController addChildViewController:newRight];
        [self moveToNewController:newRight];
    }
    
    -(void)moveToNewController:(UIViewController *) newController {
        UIView *rcView = [(ViewController *)self.parentViewController rightView];
        [self willMoveToParentViewController:nil];
        [self.parentViewController transitionFromViewController:self toViewController:newController duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{}
             completion:^(BOOL finished) {
                 [self removeFromParentViewController];
                 [rcView constrainViewEqual:newController.view];
                 [newController didMoveToParentViewController:self];
             }];
    }
    

    I found after much experimentation that the way to get the views to be the right size and to resize properly after rotation was to initially set the frame of the new controller's view to the container view's bounds, but then after the transition animation, add constraints to the new view that keep it sized right after rotation. Since this code might be used over and over, I put it in a category on UIView, with one method, constrainViewEqual:. Here is the code for that:

    -(void)constrainViewEqual:(UIView *) view {
        [view setTranslatesAutoresizingMaskIntoConstraints:NO];
        NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
        NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
        NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
        NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
        NSArray *constraints = @[con1,con2,con3,con4];
        [self addConstraints:constraints];
    }
    

提交回复
热议问题