【问题标题】:Nested view not obeying constraint on rotating parent (transform)嵌套视图不遵守旋转父级的约束(变换)
【发布时间】:2018-01-26 12:16:28
【问题描述】:

我有视频视图(父视图)和带有控件的嵌套视图(全屏按钮)

我基本上是在尝试制作一个全屏按钮,让视图旋转并变成全屏。父视图这样做没有任何问题,但子视图不遵守约束。 它与父级一起旋转,但宽度和位置不适应变换。

子视图需要固定到前导+尾随+底部。具有固定高度。

父视图也包含在包含视图中。

(videoView 是父视图)

UIView.animate(withDuration: 0.6, animations: {

            //Set the default frames ready to restore..
            self.videoViewFrame = self.videoView.frame
            self.videoViewButtonFrame = self.videoButtonView.frame


            self.navigationController?.navigationBar.layer.zPosition = -1
            self.navigationController?.setNavigationBarHidden(true, animated: true)

            self.tabBarController?.tabBar.layer.zPosition = -1
            self.tabBarController?.tabBar.isHidden = true


            self.videoView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI / 2))

            self.videoView.frame = UIScreen.main.bounds

            UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
        }

我尝试了不同的约束设置。

  • 设置前导、尾随和底部
  • 在 videoView 父级中设置相等的宽度和中心

子视图随父视图旋转,但不限制在底部、左侧、右侧。

这是控制器内裁剪后的小视图(纵向)

这里是全屏,没有按需要固定控件

如果我记录子视图的约束,动画之后约束仍然存在。日志中也没有错误 但是,如果我尝试更改子视图的框架,它不会让我这样做。 似乎约束被卡在转换发生之前的位置? setNeedsLayout 或 setNeedsUpdateConstraints 不会改变任何东西

【问题讨论】:

  • 有趣..您使用故事板或程序化方法?
  • 我使用故事板,我想我已经找到了下面的解决方案
  • 真可惜..我希望能有一个很好的挑战:)
  • 哈哈!还是谢谢

标签: ios swift constraints


【解决方案1】:

原来我需要使用 autoresizingMask

self.videoButtonView.translatesAutoresizingMaskIntoConstraints = true
self.videoButtonView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]

self.fullScreenButton.translatesAutoresizingMaskIntoConstraints = true
self.fullScreenButton.autoresizingMask = [.flexibleLeftMargin]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多