AppStoreStyleHorizontalScrollView
AppStoreStyleHorizontalScrollView copied to clipboard
Scroll View add to Table View Cell
Firstly i add the horizontalScrollView to UIView seen no problem. But Added to table view cell does not show the last items and scroll view width is beyond the container view.

here is my code...
let horizontalScrollView = ASHorizontalScrollView(frame:CGRectMake(0, 30, containerView.frame.size.width , 150))
horizontalScrollView.backgroundColor = UIColor.blueColor() //add color for testing
horizontalScrollView.leftMarginPx = 30
horizontalScrollView.miniMarginPxBetweenItems = 15
horizontalScrollView.miniAppearPxOfLastItem = containerView.frame.size.width-(200+30+50)
horizontalScrollView.uniformItemSize = CGSizeMake(200, 150)
//This must be called after changing any size or margin property of this class to get acurrate margin
horizontalScrollView.setItemsMarginOnce()
for nub in 1...7{
if let addView = NSBundle.mainBundle().loadNibNamed("ScrollViewController", owner: ScrollViewController(), options: nil)[0] as? ScrollView{
addView.titleLable.text = "\(nub)"
horizontalScrollView.addItem(addView)
}
}
containerView.addSubview(horizontalScrollView)
Your horizontalScrollView has a fixed width = containerView.frame.size.width, if you don't have auto layout code to adjust the scrollview and the container view, then it should be fit. As I can imagine, your container view do have auto layout setting on it to resize the width. But the horizontalScrollView is not adjusted correspondingly. If this is the case, I suggest your add constraints to horizontalScrollView too.
Again, your tableview cell width is limited to the table view, but the horizontalScrollView width is not, adjust your horizontalScrollView width correspondingly.
I got it. Thanks terenceLuffy 👍
Set the horizontal scroll view frame to fixed width and adjust with constraints. Its looks fine.But the last items doesn't appear fully. And margin between items is not ok in 4s but fine in 6s.

here my code...
let horizontalScrollView = ASHorizontalScrollView(frame:CGRectMake(0, 0, 300, 150))
horizontalScrollView.backgroundColor = UIColor.blueColor()
horizontalScrollView.leftMarginPx = 10
horizontalScrollView.miniMarginPxBetweenItems = 15
horizontalScrollView.miniAppearPxOfLastItem = 30
horizontalScrollView.uniformItemSize = CGSizeMake(200, 150)
//This must be called after changing any size or margin property of this class to get acurrate margin
horizontalScrollView.setItemsMarginOnce()
for nub in 1...7{
if let addView = NSBundle.mainBundle().loadNibNamed("ScrollViewController", owner: ScrollViewController(), options: nil)[0] as? ScrollView{
addView.titleLable.text = "(nub)"
horizontalScrollView.addItem(addView)
}
}
containerView.addSubview(horizontalScrollView)
horizontalScrollView.translatesAutoresizingMaskIntoConstraints = false
let viewsDictionary = ["horizontalScrollView":horizontalScrollView]
let containerView_H = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[horizontalScrollView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let containerView_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[horizontalScrollView]-10-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: viewsDictionary)
containerView.addConstraints(containerView_H)
containerView.addConstraints(containerView_V)
Your code seems correct. But you can check on two things to make sure:
- Check the content size of scrollview to see if it can hold all your 7 items and the right edge margin after all view showed. If this is the case (probably), maybe
refreshSubView
is not called when scrollview frame changed, you can try to manually call it to see if there is any difference. - Check the width of your last item to see if it is correct size
The gap differences in 6 and 4s is normal. Because the screen width is different. In 5s and below, it is 320, and 6 is 375, 6 plus is 414. Therefore, you should have different width and gap for different phones.
Hi TerenceLuffy. How can i pin the last scroll index in HorizontalScrollView. For example add the horizontalScrollView to TableView. When scroll the tableView the horizontalScrollView index lose and show the first index ever.
I guess you reuse the table view cell which contains the scroll view? I suggest you don't do that because you probably won't have same items for all scroll views? If you don't reuse the cell for other cells, it won't move the scrollview's index.