以编程方式设置约束,如故事板

时间:2018-02-22 09:35:07

标签: ios swift responsive-design viewcontroller imessage-extension

我需要设置约束以使视图等于高度乘以0.8

我使用此代码

override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
        if(presentationStyle == .compact){

                let videoController = recordingController.videoController

                let containerView = videoController?.containerView
                self.addConstraint(value: 0.1, name: "ViewAltezza",videoView: (videoController?.mainView)!, containerView: containerView!)



            print("Compact")
        }else if(presentationStyle == .expanded){

                let videoController = recordingController.videoController

                let containerView = videoController?.containerView
                self.addConstraint(value: 0.1, name: "ViewAltezza",videoView: (videoController?.mainView)!, containerView: containerView!)
            }
            print("Expand")
        }
    }

func addConstraint(value : CGFloat, name: String, videoView: UIView, containerView: UIView){
        videoView.translatesAutoresizingMaskIntoConstraints = false

        for constraint in videoView.constraints{
            if(constraint.identifier == name){

                print("Ecco la constraint \(constraint)")
                videoView.removeConstraint(constraint)

                let heightConstraint = NSLayoutConstraint(item: containerView , attribute: .height, relatedBy: .equal, toItem: videoView, attribute: .height, multiplier: value, constant: 0.0)
                heightConstraint.identifier = name
                heightConstraint.priority = UILayoutPriority(rawValue: 1000)
                print("Aggiungo questa constraint \(heightConstraint)")
                videoView.addConstraint(heightConstraint)

            }
        }

    }

代码似乎是正确的,并且调试显示初始约束和创建的约束是相同的......有任何错误吗?或者只是我忘了什么

我需要这样做,因为在iMessage扩展中,当我使用present(_viewControllerFromStoryboard ...)创建一个viewController时,当用户展开或压缩iMessage应用程序时,看起来像MSMessagesAppViewController中的视图不能自动调整大小;因此,如果有任何方法可以避免这种情况并让viewController自动修复presentationStyle,那么我将更容易解决我正在试验的问题。

0 个答案:

没有答案
相关问题