2

im new to ios programming and im trying to add a segmented controller to a scrollview because the seg controler is too big for the screen with out the scroll view scrolling horizontally. im not sure why it isnt scrolling. i think im missing something small, but i cannot find it. anything helps! thank you!

my code in the viewDidLoad:

scrollView.delegate = self
scrollView.showsHorizontalScrollIndicator = true
self.scrollView.alwaysBounceHorizontal = true
self.scrollView.scrollEnabled = true
self.scrollView.addSubview(segControl)
self.scrollView.pagingEnabled = true
self.scrollView.directionalLockEnabled = false
self.scrollView.contentSize.height = segControl.frame.height
self.scrollView.contentSize.width = segControl.frame.width
4
  • What's the value of segControl.frame.width? Is it greater than scrollView.frame.width? Commented May 18, 2016 at 16:56
  • scrollView.contentSize should be greater than scrollview.frame to make it scroll. Commented May 18, 2016 at 16:56
  • yes im 99% sure it is. how can i make that happen if it isnt? or how can i check to see if it is? Commented May 18, 2016 at 17:05
  • @user3462448 even i have same problem, i am embedding segment control inside scrollview. Scrollview is not scrolling. Can you share your solution ? Commented Aug 22, 2023 at 4:45

1 Answer 1

2

UIScrollView is pretty easy to work with: as long as its contentSize is larger than its own frame.size, it will let you scroll to unveil its subviews that are off its visible area.

Probably check the segControl.frame.width and the scrollView.frame.width values: the segControl's width should be larger for the scroll view to scroll.

It's also worth checking if the scrollView.frame is correct and is not lying somewhere off than the screen: e.g. scrollView.frame.origin.x + scrollView.frame.width is less than the screen width.

Looking at the screenshot, the scrollView.contentSize.width should be over 320 (since it's taking the whole width of the simulator screen, which is an iPhone 5/5S in your case). Since you have specified that your content width outputs as 216, looks like you are accessing the frames in the wrong callback, which is triggered when the values are not yet correctly set. I can suggest moving the

scrollView.contentSize.height = segControl.frame.height
scrollView.contentSize.width = segControl.frame.width

part to viewWillAppear() instead of viewDidLoad().

Sign up to request clarification or add additional context in comments.

9 Comments

i did, it is larger. i printed it, this is what came up in the debug area. scrollview frame width: 0.0 content size: 216.0
What about the scrollView.frame — is it ok and not off the screen by any chance?
i have the scrollview trailing and leading constraints equal to the superview. so i dont think so.
the content size is 216 but the answer to this equation scrollView.frame.origin.x + scrollView.frame.width is 300. what does this mean?
I have updated my answer with another suggestion: try moving the part where you set the contentSize to viewWillAppear. The values of the contentSize and scrollView.frame that you've provided conflict with what we see in the screenshot, so I'd say you're trying to check them in the wrong place.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.