1

I need to set constraint to have a view equal height with multiplier 0.8

I use this code

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)

            }
        }

    }

The code seems correct, and the debug shows that the initial constraint and the created constraint are the same... is there any errors ? or just I am forgetting something

I need to do this because in iMessage extension when I create a viewController using present(_viewControllerFromStoryboard . . . ), it seems like the view can't auto resize like in MSMessagesAppViewController when the user expand or compact the iMessage app; so if there is any way to avoiding this and let the viewController auto fix the presentationStyle, it will be easier for me to fix the issue I am experimenting.

  • container is inside video ???? – Shehata Gamal Feb 22 '18 at 09:43
  • yes, it is... the view is resizing I think... because all the elements disappear – Alessandro Pierro Feb 22 '18 at 09:44
  • where in code you call this function – Shehata Gamal Feb 22 '18 at 09:45
  • also call self.view.layoutIfNeeded() – Shehata Gamal Feb 22 '18 at 09:47
  • it seems like you are removing the old constraint and creating the exact same constraint again.. what is the point in that? – Milan Nosáľ Feb 22 '18 at 09:51
  • They will be different... now I set the same value of the storyboard constraint because I want to check if the code was correct – Alessandro Pierro Feb 22 '18 at 09:52
  • I think the problem is the videoView.translatesAutoresizingMaskIntoConstraints = false... because if I just set this to false, all the button I put on the view will disapear – Alessandro Pierro Feb 22 '18 at 09:54
  • Could it be because I have margin constraints on my buttons? – Alessandro Pierro Feb 22 '18 at 09:55
  • make sure that if you use autolayout, there are constraints for all the dimensions and position too.. – Milan Nosáľ Feb 22 '18 at 09:57
  • If I don't set videoView.translatesAutoresizingMaskIntoConstraints = false, all will work fine, but the view will not resize if I expand or compact the iMessage... but the elements are properly displayed until I change the view's size. So which constraint will be removed when I update translatesAutoresizingMaskIntoConstraints – Alessandro Pierro Feb 22 '18 at 10:01
  • *"...now I set the same value of the storyboard constraint..."* Are you saying you are setting constraints in **both** the Storyboard and in code for the same object? Even if not, why are you creating and removing constraints? You should just be *altering* constraints at that point - or at worst, *activate/deactivate* them. –  Feb 22 '18 at 10:16

0 Answers0