Margin Between Images in Uiscrollview

Margin between images in UIScrollView

It turns out I had an equal widths constraint set up that I'd forgotten about. This meant the multiple by which the scrollview paged was fixed at the width of the superview.

Adding a spacing between images in a UIScrollView

It's just basic mathematics..

You add UIImageView as subviews, and compute the space you want knowing the size of your subviews and the contentSize of the scrollView.

EDIT :

This basically build a gallery

-(void)photosGallery
{
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 416.0f}];
scrollView.contentSize = (CGSize){.width = view.frame.size.width, .height = 372.0f};
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
/// Build gallery
CGSize thumbSize = (CGSize){.width = 75.0f, .height = 75.0f};
const CGFloat xSpace = (scrollView.frame.size.width - (thumbSize.width * kPictsPerRow)) / (kPictsPerRow + 1); // kPictsPerRow = Number of pictures in a row
const CGFloat xInc = thumbSize.width + xSpace;
const CGFloat yInc = thumbSize.height + 15.0f;
CGFloat x = xSpace, y = 10.0f;
for (NSUInteger i = kMaxPictures; i--;)
{
UIImageView* pv = [[UIImageView alloc] initWithFrame:(CGRect){.origin.x = x, .origin.y = y, .size = thumbSize}];
[scrollView addSubview:pv];
[pv release];
x += xInc;
const NSUInteger eor = i % kPictsPerRow;

if (!eor)
y += yInc, x = xSpace;
}
[scrollView release];
}

Photos app-like gap between pages in UIScrollView with pagingEnabled

Note that this answer is quite old. The basic concept still works but
you should not be hard coding view sizes in iOS7 and 8. Even if you ignore
that advice, you should not use 480 or 330.

Have you tried making the frame of the UIScrollView slightly larger than the screen (assuming that you want to display your images fullscreen and then arranging your subviews on the same slightly-larger-than-the-screen boundaries.

#define kViewFrameWidth 330; // i.e. more than 320

CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = kViewFrameWidth;
scrollFrame.size.height = 480;

UIScrollView* myScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
myScrollView.bounces = YES;
myScrollView.pagingEnabled = YES;
myScrollView.backgroundColor = [UIColor redColor];

UIImage* leftImage = [UIImage imageNamed:@"ScrollTestImageL.png"];
UIImageView* leftView = [[UIImageView alloc] initWithImage:leftImage];
leftView.backgroundColor = [UIColor whiteColor];
leftView.frame = CGRectMake(0,0,320,480);

UIImage* rightImage = [UIImage imageNamed:@"ScrollTestImageR.png"];
UIImageView* rightView = [[UIImageView alloc] initWithImage:rightImage];
rightView.backgroundColor = [UIColor blackColor];
rightView.frame = CGRectMake(kViewFrameWidth * 2,0,320,480);

UIImage* centerImage = [UIImage imageNamed:@"ScrollTestImageC.png"];
UIImageView* centerView = [[UIImageView alloc] initWithImage:centerImage];
centerView.backgroundColor = [UIColor grayColor];
centerView.frame = CGRectMake(kViewFrameWidth,0,320,480);

[myScrollView addSubview:leftView];
[myScrollView addSubview:rightView];
[myScrollView addSubview:centerView];

[myScrollView setContentSize:CGSizeMake(kViewFrameWidth * 3, 480)];
[myScrollView setContentOffset:CGPointMake(kViewFrameWidth, 0)];

[leftView release];
[rightView release];
[centerView release];

Apologies if this doesn't compile, I tested it in a landscape app and hand edited it back to portrait. I'm sure you get the idea though. It relies on the superview clipping which for a full screen view will always be the case.

How can you add padding between views in a UIScrollView in Swift?

Your constraints are likely not behaving like you expect. My strong suggestion is to use a UIStackView instead, and bind it to the scroll view via constraints, but not your images.

let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 8 // or whatever you want

stackView.addArrangedSubview(image1)
stackView.addArrangedSubview(image2)

scrollView.addSubview(stackView)
stackView.translatesAutoresizingMask = false

// finish the constraints like the first, but for .trailing, .top, and .bottom
NSLayoutConstraint.activate([
stackView.leadingAnchor(constraintEqualTo: scrollView.leadingAnchor),
...
])

UIScrollView paging add space between pages

I had the same problem, turns out I had an equal widths constraint stopping it paging at the right distance. See my post here: Margin between images in UIScrollView - Swift version

Wried margin at right side of UIScrollView

It was simple mistake. I should give contentView.width constraint equal to Superview

contenteView.snp.makeConstraints { make in
make.edges.equalToSuperview()
make.width.equalToSuperview()
}

Swift UIScrollView - strange padding

This is caused by scrolling insets:

Click your ViewController on Storyboard and go to file inspector, and you should see this dialog:

Adjust ScrollView Insets

Untick the Adjust Scroll View Insets.

How to add space between buttons in uiscrollview

let gap: CGFloat = 0

Looks wrong, if you want a gap. Shouldn't it be e.g. ... = 15?

Alternatives could include placing them in a UIStackView with a spacing set. This would also be simple using auto-layout, creating constraints between the buttons which provide the gaps.



Related Topics



Leave a reply



Submit