多个容器视图 VS 链接到单个容器视图的多个视图控制器

Posted

技术标签:

【中文标题】多个容器视图 VS 链接到单个容器视图的多个视图控制器【英文标题】:Multiple container views VS multiple view controllers linked to a single container View 【发布时间】:2019-01-09 07:52:53 【问题描述】:

我有以下配置的视图。通过单击按钮,相应的容器视图应移动到带有动画的主屏幕。如果我处于纵向或横向模式(只有其中一种),这效果很好,但如果我尝试旋转 iphone,这将不起作用。

- (IBAction)button2Pressed:(UIButton *)button 
if(previousButton != button)
    if([previousButton.titleLabel.text isEqualToString:@"1"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width];
    else if([previousButton.titleLabel.text isEqualToString:@"3"])
        [self viewLoadedIntoContainer:self.container1.frame.size.width];
    else
        [self viewLoadedIntoContainer:self.container1.frame.size.width*2];
    previousButton = button;
    [self animateLabel:self.button2Outlet.frame.origin.x];
    indexCount=1;


- (IBAction)button1Pressed:(UIButton *)button 
if(previousButton != button)
    if([previousButton.titleLabel.text isEqualToString:@"2"])
        [self viewLoadedIntoContainer:self.container1.frame.size.width];
    else if([previousButton.titleLabel.text isEqualToString:@"3"])
        [self viewLoadedIntoContainer:self.container1.frame.size.width*2];
    else
        [self viewLoadedIntoContainer:self.container1.frame.size.width*3];

    previousButton = button;

    [self animateLabel:self.button1Outlet.frame.origin.x];
    indexCount=0;


- (IBAction)button3Pressed:(UIButton *)button 
if(previousButton != button)
    if([previousButton.titleLabel.text isEqualToString:@"1"])
       [self viewLoadedIntoContainer:-self.container1.frame.size.width*2];
    else if([previousButton.titleLabel.text isEqualToString:@"2"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width];
    else
       [self viewLoadedIntoContainer:self.container1.frame.size.width];
    previousButton = button;

    [self animateLabel:self.button3Outlet.frame.origin.x];
    indexCount=2;


- (IBAction)button4Pressed:(UIButton *)button 
if(previousButton != button)
    if([previousButton.titleLabel.text isEqualToString:@"3"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width];
    else if([previousButton.titleLabel.text isEqualToString:@"2"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width*2];
    else
        [self viewLoadedIntoContainer:-self.container1.frame.size.width*3];
    previousButton = button;
    [self animateLabel:self.button4Outlet.frame.origin.x];
    indexCount=3;


-(void)viewLoadedIntoContainer:(CGFloat)x
 __weak __typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3f animations:^

    weakSelf.container1.layer.position = CGPointMake(weakSelf.container1.layer.position.x+(x), weakSelf.container1.layer.position.y);
    weakSelf.container2.layer.position = CGPointMake(weakSelf.container2.layer.position.x+(x), weakSelf.container2.layer.position.y);
    weakSelf.container3.layer.position = CGPointMake(weakSelf.container3.layer.position.x+(x), weakSelf.container3.layer.position.y);
    weakSelf.container4.layer.position = CGPointMake(weakSelf.container4.layer.position.x+(x), weakSelf.container4.layer.position.y);
    //[weakSelf.container1 setFrame:CGRectMake(weakSelf.container1.frame.origin.x+(x), weakSelf.container1.frame.origin.y, weakSelf.container1.frame.size.width, weakSelf.container1.frame.size.height)];

    //[weakSelf.container2 setFrame:CGRectMake(weakSelf.container2.frame.origin.x+(x), weakSelf.container2.frame.origin.y, weakSelf.container2.frame.size.width, weakSelf.container2.frame.size.height)];

    //[weakSelf.container3 setFrame:CGRectMake(weakSelf.container3.frame.origin.x+(x), weakSelf.container3.frame.origin.y, weakSelf.container3.frame.size.width, weakSelf.container3.frame.size.height)];

    //[weakSelf.container4 setFrame:CGRectMake(weakSelf.container4.frame.origin.x+(x), weakSelf.container4.frame.origin.y, weakSelf.container4.frame.size.width, weakSelf.container4.frame.size.height)];

completion:^(BOOL finished) 
    //[weakSelf.container1 layoutIfNeeded];
];

我正在使用 4 个容器视图。我是否应该使用 4 个高度和宽度相同且彼此重叠的容器视图。或者我应该使用 4 个使用自定义 segue 链接到单个容器视图的视图控制器?

【问题讨论】:

【参考方案1】:

这完全取决于每个视图/视图控制器的具体功能。

但一般来说;我通常让每个“屏幕”(全帧视图)都有自己的 UIViewController。我通常制作屏幕 UIViews 的子视图。

【讨论】:

我已经编辑了我的问题。你能看到我在这里做错了什么吗?

以上是关于多个容器视图 VS 链接到单个容器视图的多个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

将多个容器视图放在 ios 中的分页 UIScrollView 中

具有多个子视图问题的容器视图

将多个单独的 Swift 文件加载到主容器视图中

如何从容器/父视图控制器和多个子视图控制器共享数据数组

使用自动布局将具有不同高度的多个视图居中

将 @State 属性的 @Binding 值从容器视图传递到其子视图