简体   繁体   English

iOS自定义Segue和ContainerView

[英]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 ; 我有三个按钮和一个UIView (我称之为containerView ),点击每个按钮, containerView将通过自定义序列在UIViewController显示一个View,这些按钮使用一个IBAction方法( switchView ),我switchView三个按钮放入一个名为navButtonsIBOutletCollection ; and in viewDidLoad I call switchView to make the containerView show first view controller. viewDidLoad我调用switchView使containerView显示第一个视图控制器。

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. 并且代码运行良好,没有错误,唯一的问题是当我点击第一个按钮时,第一个UIViewController将显示在containerView ,UIImageView(按钮,标签等)应位于屏幕的中央,但它的行为永远不会像这个在第一次加载时,当我点击另一个按钮然后再次点击第一个按钮时,它的表现与预期的一样,我不知道发生了什么,第一次加载与再次点击之间有什么区别。 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; 在controller.currentController = destController之前;

在UIViewController中设置了clipSubViews吗?

Solved. 解决了。 Thanks for the answer of Bojan Bozovic ; 感谢Bojan Bozovic的回答;

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

Add above before controller.currentController = destController; 在controller.currentController = destController;之前添加以上内容;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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