【问题标题】:ios - Resize Custom Views programmaticallyios - 以编程方式调整自定义视图的大小
【发布时间】:2012-11-09 07:18:52
【问题描述】:

这只是一个测试项目,在我实现更大的东西之前:-)

根据图像,我在 1 个 ViewController 中创建了 2 个自定义视图。我需要它们重叠或至少移开并再次在应用程序内返回。

我已经尝试查找自定义视图,但我很幸运。

我希望使用此代码或类似的代码:

-(IBAction)move02Action
{
   [UIView beginAnimations:nil context:NULL];
   [UIView setAnimationDuration:0.5];
       _left.view.frame = CGRectMake(80, 70, 160, 380);  //_left is Left UIViewContorller
       _right.view.frame = CGRectMake(240, 70, 160, 380); //_right is Right UIViewContorller
   [UIView commitAnimations];
}

我意识到这是针对 UIViews 而不是 View Controllers。关于在哪里看有什么想法吗?

感谢您的洞察力:-)

【问题讨论】:

    标签: iphone ios uiviewcontroller storyboard containers


    【解决方案1】:

    你可以像这样使用 Containment 视图控制器

     UIView *view = [[UIView alloc] initWithFrame:CGRectMake((80, 70, 160, 380))];
     [view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    
    LeftViewController *left = [[LeftViewController alloc] init];
    
    left.view.frame = view.bounds; 
    [left  setDelegate:self];
    [view addSubview:left .view];
    
    [left  didMoveToParentViewController:self];
    [self addChildViewController:left ];
    

    查看"Implementing a Container View Controller" 文档。

    编辑

    1) 在 self.view 中添加一个 ScrollView,并在不同帧的滚动视图中添加两个视图 使用上面建议的代码添加视图。

    2) 像这样在必要时为滚动视图设置内容关闭集

     CGPoint bottomOffset = CGPointMake(scrolposition, scrollView.contentSize.height - scrollnView.bounds.size.height);
        [scrollView setContentOffset:bottomOffset animated:NO];
    

    如果您想要重叠视图的效果,请使用 [[self view] bringSubviewToFront:[self LeftView]];[[self view] sendSubviewToBack:LeftView];

    【讨论】:

    • 我一直在玩弄你的答案,但这增加了第三个视图并且确实操纵了已经到位的第二个。
    • 好的,我会调查的 - 更令人费解:-) 等我搞砸了我会更新它
    • 我最终没有使用 UIScroll,但 UIView 的东西让我朝着正确的方向前进,谢谢:-)
    猜你喜欢
    • 2011-02-27
    • 2015-05-26
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多