IOS 自定义 Segue 和 ContainerView

标签 ios objective-c segue

我有三个按钮和一个 UIView(我称它为 containerView),点击每个按钮,containerView 将显示一个 View 在通过自定义 segue 的 UIViewController 中,按钮使用一个 IBAction 方法(switchView),我将三个按钮放在 IBOutletCollection 中 调用了 navButtons; 在 viewDidLoad 中,我调用 switchView 使 containerView 显示第一个 View Controller 。

代码运行良好,没有错误,唯一的问题是当我点击第一个按钮时,第一个 UIViewController 将显示在 containerView 中,UIImageView(按钮,标签等)应该在屏幕的中央,但它在第一次加载时从来没有像这样,当我点击另一个按钮然后再次点击第一个按钮时,它的行为符合预期, 我不知道发生了什么,第一次加载和再次点击之间有什么区别。 我在代码中遗漏了什么吗?

我英语不好,有什么不明白的地方请见谅。

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

最佳答案

我认为你应该添加:

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

之前 controller.currentController = destController;

关于IOS 自定义 Segue 和 ContainerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31337819/

相关文章:

ios - 在 Swift 中 segue 之后从堆栈中移除(删除) View /viewController 的正确方法

ios - 尝试在...上呈现...其 View 不在窗口层次结构中

ios - 找不到此设备的有效配置文件,Xcode 6?

iOS:加载表格 View 时无法更新单元格中 View 的框架

ios - 在 iOS 中为 AVCaptureDevice 添加过饱和效果

objective-c - AVCaptureSession和AudioQueue

ios - ViewController 不符合协议(protocol) UITableViewDataSource

ios - 在两个 ViewController 之间传递数据时出错

ios - NSMutableArray 空串()

ios - 在 Swift 中更改搜索栏占位符文本字体