简体   繁体   中英

How do I resize a UIScrollView so that it's not 100% of the parent UIView?

I've built an app using the UITabBar template. I have a few tabbar items, one item displays a view. That view has a UIScrollView element that has paging enabled to mimic the behaviour of the iPhone springboard ie pages that can be scrolled left to right.

I'm trying to drop in a UIPageControl, so I've resize the UIScrollView so that it's slightly shorter than the parent UIView height and have placed a UIPageControl below it.

When I run the app the UIScrollView is always 100% of the height of the parent UIView and I can't see the UIPageControl.

I've got the following code in my viewDidLoad method of the view controller for the tab:

UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(640,377);  

This sets the content size ok and I can scroll left to right. I've tried adding:

tempScrollView.frame=CGRectMake(0, 0, 640, 377);

To to resize the scroll view but it still shows 100%. See diagram below showing the issue:

例

I think you shouldn't resize the frame to 640, 377 because it would make the paging stop working, once the contentSize would be the same as the frame size.

One solution would be to set the desired frame size in the interface builder (like the left most figure) and set proper autosizing masks. I gues what you are looking for is the configuration below

最重要的部分是锁定的下边距

To check if the changes are working, I would use a uiscrollview of height visibly smaller, just to make sure the behaviour is the desired.

If you want your view to scroll you need to change the tempScrollView.contentSize to be bigger than tempScrollView.frame

If you do this:

CGFloat contentWidth = tempScrollView.frame.size.width*2;
tempScrollView.contentSize=CGSizeMake(contentWidth,377);

You will have 2 pages.

You need to activate paging too with:

[tempScrollView setPagingEnabled:TRUE];

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