Gintas_
Gintas_

Reputation: 5030

Resize UIScrollView's height to screen height?

enter image description here

Black - screen frame/size

Red - default UIScrollView's frame/size

Yellow - my button.

I want to always keep that button at the bottom. So e.g. on 3.5in screen, scrolling is available and button is at the bottom, no problem. Now, when we move to 5.5in screen, it becomes like in this image, button is not at the bottom anymore. What I am trying to do is change UIScrollView's height, but it does not work:

if(self.view.frame.height > contentView.frame.height)
    {
        print("fixing scroll view")
        contentView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 2)
        scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height * 2)
    }

self.view.frame.height * 2 is just for testing. What could be wrong?

Upvotes: 2

Views: 2238

Answers (2)

Edudjr
Edudjr

Reputation: 1835

Late response, but for those who need a button to stay at the bottom of the screen in a scrollview, if the content view should be the same as the screen height, try this:

  1. Set ScrollView top, left, right and bottom constraints to 0 from parent
  2. Set ContentView to Equal Width and Equal Height of ScrollView
  3. Set ContentView top, left, right and bottom constraints to 0 from ScrollView
  4. Give each element inside the scroll view some constraints, linking everything from top to bottom
  5. Select one constraint between the elements and remove it, or make it low priority. I'd say to make it low priority because if you have to delete the Equal Height, the layout won't break.

enter image description here

Upvotes: 2

TheValyreanGroup
TheValyreanGroup

Reputation: 3599

With auto layout all you need to do is add pin constraints to your scrollView to each corner of the superview.

Auto layout pins

Set your 4 constraints to 0 and you'll be all set.

Upvotes: 0

Related Questions