简体   繁体   中英

IOS Custom Segue and ContainerView

I have three buttons and a UIView (I call it containerView ), tap each of the buttons, the containerView will show a View in a UIViewController through a custom segue, the buttons use one IBAction method( switchView ), I put the three buttons in a IBOutletCollection called navButtons ; and in viewDidLoad I call switchView to make the containerView show first view controller.

And the code run well, no error,the only question is when I tap first button,the first UIViewController will shown in containerView , the UIImageView (buttons,labels,etc) should be in the centre of the screen, but it never behave like this when it's first loaded, when I tap another button then tap first button again,it behaved as expected, I have no idea what happened and what's the difference between first load and tapping it again. IS THERE ANYTHING I MISSED IN THE CODE ?

I am not good at English,Sorry for anything unclear .

- (void)viewDidLoad {
    [super viewDidLoad];
    self.availableIdentifier = [[NSMutableArray alloc] initWithObjects:@"Seg1",@"Seg2",@"Seg3", nil];


    [self switchView:self.navButtons[0]];
}

- (IBAction)switchView:(UIButton *)sender
{

    [self setSelectedIndexs:(int)sender.tag];

}

- (void)setSelectedIndexs:(int)index
{

    [self performSegueWithIdentifier:self.availableIdentifier[index] sender:self.navButtons[index]];

}

//Code of Custom Segue:
-(void)perform
{
    ViewController *controller = (ViewController *)self.sourceViewController;
    UIViewController *destController = (UIViewController *)self.destinationViewController;
    for (UIView *view in controller.containerView.subviews)
    {
        [view removeFromSuperview];
    }

    controller.currentController = destController;
    [controller.containerView addSubview:destController.view];

    [controller.containerView setTranslatesAutoresizingMaskIntoConstraints:NO];


    [destController didMoveToParentViewController:controller];
}

I think you should add:

destController.view.frame = controller.containerView.frame;

before controller.currentController = destController;

在UIViewController中设置了clipSubViews吗?

Solved. Thanks for the answer of Bojan Bozovic ;

CGRect frame = controller.containerView.frame;
frame.origin.y = 0;
destController.view.frame = frame;

Add above before controller.currentController = destController;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM