How do I customize a half-height view?

I want to set the height of view1 to half the height of the screen for all devices (when the device is in portrait mode)

Here's what I want it to look like:

enter image description here

so i am doing auto height layout of View1

    @IBOutlet weak var heighConstraint: NSLayoutConstraint!

      

my viewwillappear here:

override func viewWillAppear(animated: Bool) {
        self. heighConstraint.constant = self.view.frame.size.height / 2
}

      

But it didn't work when I started the application. what is wrong here?

+3


source to share


3 answers


Try changing the value of the heightConstraint

constant in the method viewDidLayoutSubviews

.



override func viewDidLayoutSubviews() {
    heightConstraint.constant = self.view.frame.size.height / 2
}

      

+7


source


I know the accepted answer is correct, but there is an easier way to do it -

Add a view (the one shown in yellow). Connect it to the three edges (top, front and back). I set it to 0, but changed it to suit your needs.

enter image description here

Create a height equal to the constraint on main. Kind as

enter image description here



enter image description here

Open this constraint in Inspector view and edit the multiplier as 0.5. By setting it to 0.5, the height value will be half the height of the mainView.

enter image description here

Just make sure FirstItem = View1 and SecondItem = SuperView as shown in the image.

+8


source


Please try this. Hope this helps you. The bottom line will accept the device boundaries and set the frame or height to match it.

view1.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/2);

      

Hope this helps !!! Good luck ...

0


source







All Articles