Uiscrollview Horizontal Paging Like Mobile Safari Tabs

UIScrollView horizontal paging like Mobile Safari tabs

A UIScrollView with paging enabled will stop at multiples of its frame width (or height). So the first step is to figure out how wide you want your pages to be. Make that the width of the UIScrollView. Then, set your subview's sizes however big you need them to be, and set their centers based on multiples of the UIScrollView's width.

Then, since you want to see the other pages, of course, set clipsToBounds to NO as mhjoy stated. The trick part now is getting it to scroll when the user starts the drag outside the range of the UIScrollView's frame. My solution (when I had to do this very recently) was as follows:

Create a UIView subclass (i.e. ClipView) that will contain the UIScrollView and it's subviews. Essentially, it should have the frame of what you would assume the UIScrollView would have under normal circumstances. Place the UIScrollView in the center of the ClipView. Make sure the ClipView's clipsToBounds is set to YES if its width is less than that of its parent view. Also, the ClipView needs a reference to the UIScrollView.

The final step is to override - (UIView *)hitTest:withEvent: inside the ClipView.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return [self pointInside:point withEvent:event] ? scrollView : nil;
}

This basically expands the touch area of the UIScrollView to the frame of its parent's view, exactly what you need.

Another option would be to subclass UIScrollView and override its - (BOOL)pointInside:(CGPoint) point withEvent:(UIEvent *) event method, however you will still need a container view to do the clipping, and it may be difficult to determine when to return YES based only on the UIScrollView's frame.

NOTE: You should also take a look at Juri Pakaste's hitTest:withEvent: modification if you are having issues with subview user interaction.

iOS : How to do proper paging in UIScrollView?

Paging scroll views alway page multiples of their frame size. So in your example paging is always +320.

This behavior is good if you have content portions matching the frame of the scroll view.

What you have to do, is giving your scroll view a width of 213 and set its clipsToBounds property to NO.
After that your scroll view pages exactly how you want and you see what's left and right outside the frame.
Additionally you have to do a trick to make this left and right area delegate touches to the scroll view.

It's greatly explained in this answer.

UIScrollView with horizontal paging using UIView as Subviews

    let scrollView : UIScrollView = UIScrollView(frame: CGRect(x: 80, y: 80, 
width: 250, height: 300))
scrollView.isPagingEnabled = true
scrollView.backgroundColor = .orange
view.addSubview(scrollView)
let numberOfPages :Int = 5
let padding : CGFloat = 15
let viewWidth = scrollView.frame.size.width - 2 * padding
let viewHeight = scrollView.frame.size.height - 2 * padding

var x : CGFloat = 0

for i in 0...numberOfPages{
let view: UIView = UIView(frame: CGRect(x: x + padding, y: padding, width: viewWidth, height: viewHeight))
view.backgroundColor = UIColor.white
scrollView .addSubview(view)

x = view.frame.origin.x + viewWidth + padding
}

scrollView.contentSize = CGSize(width:x+padding, height:scrollView.frame.size.height)

UIScrollView with pagination + showing part of the previous/following pages

For your reference, you can see a sample implementation of a page control from here.
https://developer.apple.com/library/content/samplecode/PageControl/Introduction/Intro.html

For the implementation you want,
to your surprise, the scrollview's width is actually smaller than 320 (or 480). The magic property to set is:

scrollView.clipsToBounds = NO

The only problem with this implementation is that the scrollview get no touch events if the touch is outside the bounds of the scrollView. This can be fix by passing its parent hitTest event to scrollView.

Just to link to to a better explanation:
UIScrollView horizontal paging like Mobile Safari tabs

Slightly different from what I recommend but does the same thing.

Edit:

I have a small project called LXPagingViews that does the above, hopefully in an out of the box manner (Do drop me a pull request or feedback in issue): https://github.com/lxcid/LXPagingViews

UIScrollView paging from center to left and right

You should make 3 pages, so the contentSize should be 3 times the size of one page, as far as i can see, that is happening.

Now make sure the user always starts on page 2 (the center page), do this by setting the contentOffset to 1x the width of the scrollView for the x coordinate and 0 for the y coordinate.

After that, the user should be able to scroll left and right.

PS: by looking at your code, it seems like you are assuming everybody uses an iPhone 6 (width of 375pt), you might want to use the calculated width (by using self.view.frame.size.width or something relative)

Paging UIScrollView in increments smaller than frame size

Try making your scrollview less than the size of the screen (width-wise), but uncheck the "Clip Subviews" checkbox in IB. Then, overlay a transparent, userInteractionEnabled = NO view on top of it (at full width), which overrides hitTest:withEvent: to return your scroll view. That should give you what you're looking for. See this answer for more details.

Adding images to horizontal paging UIScrollView

The same way you are setting label1.text.

First, you need to be sure your UIImageView object is linked to an IBOutlet, so you can access it via code. I'm assuming you already know how to do it, since you are access label1 already.

Afterwards, just instantiate your UIImage like this:

slide1.imageView.image = UIImage(named: "tutorial1")

I hope it helps.



Related Topics



Leave a reply



Submit