【问题标题】:iOS programmatically alter uiview layoutiOS 以编程方式更改 uiview 布局
【发布时间】:2013-03-26 22:15:35
【问题描述】:

我的 iOS 应用上有一个页面,其布局基于下图,如下所示。

  • 深灰色框 = 导航栏
  • 浅灰色框 = UIView(用于演示,带有一些漂亮的阴影等)
  • 黑色边框 = UIView(同样,具有漂亮的阴影、不同的背景颜色等)
  • 2 个文本字段
  • 粉色框 = 事件后显示的另一个视图。

目前,我正在使用 initWithFrame 布局所有内容,并明确设置每个项目的框架,我知道这可能不是很好(我是 ios 开发新手,所以请指出正确的方向)。

我想要实现的是,当在第二个文本字段中输入一些文本时,粉红色框会显示出来,超级视图会正确响应,并在新字段周围很好地布局。

我对事件本身没有任何问题,我可以轻松地依次更改每个 UIView 的框架,但我觉得这是错误的,并且希望得到一些指示。我最终还希望粉红色的盒子在其中制作动画,因此包含的视图也可以用它制作动画。

我希望我已经彻底解释了自己。非常感谢

【问题讨论】:

  • 你读过View Programming Guide for iOS 吗?较新的方法是Handling Layout Changes Automatically Using Autoresizing Rules,通用的是Tweaking the Layout of Your Views Manually
  • 你应该在你的 UIView 子类中实现 layoutSubviews 并在那里设置视图框架
  • 感谢@A-Live,该文档真的很有帮助。我用 autoresizesSubviews = YES! 对其进行了排序!

标签: ios objective-c cocoa-touch uiview uikit


【解决方案1】:

如果您有想要触发的事件,然后在后面运行动画,这是一件非常简单的事情。

- (void)myEventMethod:(id)sender {
  // Do your event stuff here
  // Animate the pink box into view
  // The below assumes the pink box is hidden behind the blackView and you'd like to drop it down 44 pixels over 0.3 seconds - hopefully correct - haven't tested
  // need to be careful as it'll move down 44px every time myEventMethod: is triggered
  [UIView animateWithDuration:0.3
                        delay:0.0
                      options:UIViewAnimationOptionCurveEaseIn
                   animations:^{
                   CGRect pinkboxEndFrame = CGRectMake(self.blackView.frame.origin.x, self.blackView.frame.origin.y + 44, self.pinkbox.frame.size.width, self.pinkbox.frame.size.height);
                   [self.pinkbox setFrame:pinkboxEndFrame];
                   }
                   completion:nil];

}

只需使用您想要对其他视图执行的任何其他动画更新上述内容即可。

有关动画视图的更多信息,您可以查看 Apple 的 View Programming Guide

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多