简体   繁体   中英

How do I add UIView on UIScrollView

I wanted to add some labels and textfields on my view and that view should scroll, so I am thinking of putting view on scrollview.

  1. I wanted to add some labels and textfields on my view
  2. And that view should scroll
  3. So I am thinking of putting scrollView
  4. And then I wanted to put my view having labels and Textfields
  5. Is it possible?

Please check scrollview property Bounces Vertically is checked or not if not check it 在此处输入图片说明

可能是您的内容少于ScrollView ,这就是禁用滚动的原因

Put view with labels in scrollview. Scrollview should be less in height than view. View should have height considering all ui cvontrols in it. should work like this

Make sure your scroll view height is not greater than view its content size has to be greater as all people says. You get confused between content size and scroll view height. Meas using nib suppose your scroll view height is 300 and when you use the code content size should be greater than 480 so it will be scroll-able on iPhone 4 size device who's height is 480

To make sure your constraints are well defined, first check these steps:

  • Make sure your base view is a UIView
  • Put into a UIScrollView and add constraints to the edges of UIView
  • Put into the scrollview a UIView and add constraints to the edges of the scrollview. I call this "content view".
  • You should assign a width to the content view. I usually set the width of the content view equal to the width of the scrollview.
  • Put your labels/views/images/whatever into the content view and use autolayout constraints to resize them automatically to fit the target screen.

Please consider that:

  • Your scrollview must always have a height, fixed or dynamic. In order to avoid errors with autolayout, consider that:
    • the last element on the bottom of the content view must always have a constraint to the bottom edge of the scrollview, or
    • the content view must have a fixed height
  • If the height of your content view is less than the height of the scrollview, the view will not scroll. You should add more views or more margin to the bottom constraint of the content view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM