Fallacy Walker
Fallacy Walker

Reputation: 23

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];
}

Upvotes: 1

Views: 106

Answers (3)

Fallacy Walker
Fallacy Walker

Reputation: 23

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;

Upvotes: 0

Bojan Bozovic
Bojan Bozovic

Reputation: 910

I think you should add:

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

before controller.currentController = destController;

Upvotes: 3

Misha
Misha

Reputation: 685

is your clipSubViews set in UIViewController ?

Upvotes: 0

Related Questions