美文网首页
Contrainer View - storyboard 的 a

Contrainer View - storyboard 的 a

作者: 居然是村长 | 来源:发表于2016-03-31 21:51 被阅读340次

    Contrainer View storyboard 能拖出来的控件,继承UIView,当又与UIStackView 不一样,很奇葩的东西。

    注1:可以直接拖入某个ViewController,并且可以多个!每一个对应连接了一个VC,相当于add childViewController了!
    注2:链接有segue,但是不能直接用会崩溃,也不知道该怎么用才好。
    注3:现在只能遍历自身的childViewControllers,然后去切换,感觉并没有纯代码方便啊,阿西吧。

    // 找到对应的 childViewController(总觉得哪里不对!!!!)
    - (void)checkChildViewController {
        [self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj isKindOfClass:[AViewController class]]) {
                self.avc = obj;
            }
            else if ([obj isKindOfClass:[BViewController class]]) {
                self.bvc = obj;
            }
            else if ([obj isKindOfClass:[CViewController class]]) {
                self.cvc = obj;
                self.currendViewController = obj;// 最后一个拖进去的 container View 是当前的
            }
            else {
                // if  more vc
            }
        }];
    }
    
    // 切换
    - (void)transitionToViewController:(UIViewController *)destinationViewController {
        if (destinationViewController == self.currendViewController) {
            return;
        }
        [self transitionFromViewController:self.currendViewController toViewController:destinationViewController duration:CGFLOAT_MIN options:UIViewAnimationOptionCurveEaseOut animations:^{
            
        } completion:^(BOOL finished) {
            
            self.currendViewController = destinationViewController;
        }];
    }
    

    顺便贴个纯代码的

        self.viewControllers = @[self.detailViewController,
                                 self.performanceViewController,
                                 self.relatedDocumentViewController,
                                 self.similarProductViewController,
                                 self.productAboutViewController];    
    
    - (void)addChildViewControllers {
        
        int temp = 0;
        for (UIViewController *viewController in self.viewControllers) {
            [self addChildViewController:viewController];
            [viewController didMoveToParentViewController:self];
            viewController.view.frame = self.contentView.bounds;
            
            if (temp == 0) {
                [self.contentView addSubview:viewController.view];
                self.currentViewController = viewController;
            }
            temp ++;
        }
    }
    
    - (void)transitionViewControlleToIndex:(NSInteger)index {
        
        if (index >= self.viewControllers.count) {
            return ;
        }
        
        UIViewController *transitionedController = self.viewControllers[index];
        if (transitionedController != self.currentViewController) {
            
            [self transitionFromViewController:self.currentViewController
                              toViewController:transitionedController
                                      duration:CGFLOAT_MIN
                                       options:UIViewAnimationOptionCurveEaseInOut
                                    animations:^{
                                        //
                                    } completion:^(BOOL finished) {
                                        self.currentViewController = transitionedController;
                                    }];
        }
    }
    

    1

    相关文章

      网友评论

          本文标题:Contrainer View - storyboard 的 a

          本文链接:https://www.haomeiwen.com/subject/inhtlttx.html