Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How make elements centered if they all in bounds of scrollview? #2

Open
andrewSvsg opened this issue Apr 2, 2016 · 1 comment
Open

Comments

@andrewSvsg
Copy link

Now this situation
[item1_item2_item3______ ]
I want make like this
[_item1_item2_item3]

Thanks!

@terenceLuffy
Copy link
Owner

terenceLuffy commented Apr 2, 2016

The default behaviour is not to centre the sub views, so the current control can't do the job you want. Instead, you can do a little bit change to achieve your target.

Add the following function to ASHorizontalScrollview.swift, and call this function in refreshSubView (before self.contentSize is set) as well as where you want to centre the subviews.

public func centerSubviews() -> CGFloat{
        if let itemLastX = self.items.last?.frame.maxX {
            if itemLastX + self.leftMarginPx < self.frame.size.width {
                let extraGap = (self.frame.size.width - itemLastX - self.leftMarginPx) / 2
                var itemX = self.leftMarginPx + extraGap
                for item in self.items
                {
                    item.frame = CGRectMake(itemX, item.frame.origin.y, item.frame.width, item.frame.height)
                    itemX += item.frame.width + self.itemsMargin
                }
                return itemX - self.itemsMargin + self.leftMarginPx + extraGap;
            }
            return itemLastX - self.itemsMargin + self.leftMarginPx
        }
        return 0
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants