AppStoreStyleHorizontalScrollView icon indicating copy to clipboard operation
AppStoreStyleHorizontalScrollView copied to clipboard

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

Open andrewSvsg opened this issue 8 years ago • 1 comments

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

Thanks!

andrewSvsg avatar Apr 02 '16 08:04 andrewSvsg

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
    }

terenceLuffy avatar Apr 02 '16 21:04 terenceLuffy