【问题标题】:How can I resize a view, while proportionally resizing it's subviews?如何调整视图的大小,同时按比例调整其子视图的大小?
【发布时间】:2015-12-13 06:54:30
【问题描述】:

我正在尝试创建一个自定义 segue,其中第一个视图控制器缩小一点,第二个在它上面滑动。以下是我的 segue 实现:

override func perform() {
    let firstView = self.sourceViewController.view as UIView!
    let secondView = self.destinationViewController.view as UIView!
    let screenSize = UIScreen.mainScreen().bounds.size
    let window = UIApplication.sharedApplication().keyWindow

    secondView.frame = CGRectMake(0.0, screenSize.height, screenSize.width, screenSize.height)
    window?.insertSubview(secondView, aboveSubview: firstView)

    UIView.animateWithDuration(2, delay: 0, options: .CurveEaseIn, animations: {

        // BEGIN RELEVANT PORTION
        let offset: CGFloat = 20

        firstView.frame = CGRectMake(offset, offset, screenSize.width - (offset * 2), screenSize.height - (offset * 2))
        firstView.autoresizesSubviews = true
        firstView.layoutIfNeeded()
        // END RELEVANT PORTION

    }, completion: nil)

    UIView.animateWithDuration(5, delay: 1, options: .CurveEaseOut, animations: {

        secondView.frame = CGRectMake(0.0, 0.0, screenSize.width, screenSize.height)

    }, completion: nil)
}

这会产生如下结果:

这已经很棒了,但不是我想要的。我需要子视图(“嗨,我是 Matt。”标签和按钮)与父视图成比例地调整大小,就好像整个视图都在 z 轴上下降一样。

我正在使用自动布局。

非常感谢您!

【问题讨论】:

  • 你想让标签和按钮相对于它的 superView 缩小吗?
  • @MuhammadWaqasBhati 是的,这就是我想要做的。 :)
  • 您是否在标签和按钮上添加了约束及其超级视图?
  • 是的。它们都在超级视图约束中具有中心,标签的水平中心有一个常数
  • 我已经在下面添加了答案,告诉我它是否不起作用

标签: ios swift uiview autolayout


【解决方案1】:

我使用CGAffineTransform解决了这个问题。

override func perform() {
    let firstView = self.sourceViewController.view as UIView!
    let secondView = self.destinationViewController.view as UIView!
    let screenSize = UIScreen.mainScreen().bounds.size
    let window = UIApplication.sharedApplication().keyWindow

    secondView.frame = CGRectMake(0.0, screenSize.height, screenSize.width, screenSize.height)
    window?.insertSubview(secondView, aboveSubview: firstView)

    firstView.layer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
    firstView.layer.position = CGPoint(x: 0.0, y: 0.0)

    UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseInOut, animations: {

        print(firstView.frame)

        let offset: CGFloat = 25
        let offsetX = 2 * (offset / firstView.frame.width)
        let offsetY = 2 * (offset * (firstView.frame.height / firstView.frame.width) / firstView.frame.height)

        let transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - offsetX, 1 - offsetY)
        firstView.transform = transform
        firstView.layer.position = CGPoint(x: offset, y: offset)

    }, completion: nil)

    UIView.animateWithDuration(0.5, delay: 0.05, options: .CurveEaseOut, animations: {

        secondView.frame = CGRectMake(0.0, 0.0, screenSize.width, screenSize.height)

        }) { finished -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
    }
}

谢谢大家的帮助!

【讨论】:

    【解决方案2】:

    应用变换是实现它的好方法。

    还有另一种方法可以实现它,而不会修改第一个视图控制器的视图框架。拍摄第一个 viewcontroller 视图的快照并使用它而不是视图本身。您可以在 UIView 中使用以下方法来拍摄快照:

    - (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates;
    

    【讨论】:

      【解决方案3】:

      我在 Xcode 中使用您的场景进行了尝试,现在您必须按照以下步骤操作

      1- Give UIButton & UILabel Leading and Trailing Space to their SuperView
      2- Make UIButton central Vertically
      3- Give Vertical Space from UIButton to UILabel
      

      重要:

      希望它会起作用,因为技巧是您必须为超级视图提供前导和尾随空间,以使 InnerViews 收缩或扩展其超级视图

      【讨论】:

        【解决方案4】:

        一种方法可以是:

        • 给它们(标签和按钮)宽度限制。
        • 制作两个宽度限制的出口。 constraintWidthLabelconstraintWidthButton
        • 您可以像这样解除约束

        • 当您执行 segue 时,您必须通过动画将这些宽度约束设为零。

          UIView.animateWithDuration(2, delay: 0, options: .CurveEaseIn, animations: {
          
          // BEGIN RELEVANT PORTION
          let offset: CGFloat = 20
          constraintWidthLabel.constant = 0
          constraintWidthButton.constant = 0;
          
          firstView.frame = CGRectMake(offset, offset, screenSize.width - (offset * 2), screenSize.height - (offset * 2))
          firstView.autoresizesSubviews = true
          firstView.layoutIfNeeded()
          // END RELEVANT PORTION
          
          }, completion: nil)
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多