ASHorizo​​ntalScrollView未显示所有元素

时间:2017-02-26 21:55:23

标签: ios uitableview scrollview

我正在尝试实现ASHorizo​​ntalScrollView(就像App Strore水平滚动菜单一样)。我调整了它的框架,但最后一个元素从未显示过,它的大小看起来比超级视图大。你可以在那里看到它的最后一个元素,但永远不会把它带上。

I touch and drag to force to show the last element

我将水平滚动视图的大小配置为它所分配的表格单元的大小。

我的问题是我如何设置HORIZONTAL SCROLLVIEW宽度以显示所有元素?

这是我在CellForRowAtIndexPath中设置它的方式:

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier  = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

        //First row must be ASHorizontalScrollView
        if indexPath.row == 0 {
            let horizontalScrollView = ASHorizontalScrollView.init(frame: CGRect.init(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height))


            //sample code of how to use this scroll view
            horizontalScrollView.uniformItemSize = CGSize(width: 74.4, height: 74.4)

            //this must be called after changing any size or margin property of this class to get acurrate margin
            horizontalScrollView.setItemsMarginOnce()
            //create buttons for cell 1
            var buttons = [UIView]()


            var choosedColor : UIColor!
            //Loop creating buttons and  customizing them
            for i in 0...contacts.count - 1 {

                var aButton = CustomButton.init(frame: CGRect.init(x: 0, y: 0, width: 74, height: 74))

                //Choose a color depending of the index it's showing
                switch i % contacts.count {
                case 0:
                    choosedColor = UIColor.init(red: 0.639, green: 0.780, blue: 0.294, alpha: 1.0)
                    break
                case 1:
                    choosedColor = UIColor.init(red: 0.807, green: 0.258, blue: 0.317, alpha: 1.0)
                    break
                case 2:
                    choosedColor = UIColor.init(red: 0.286, green: 0.509, blue: 0.772, alpha: 1.0)
                    break
                default:
                    choosedColor = UIColor.init(red: 0.639, green: 0.780, blue: 0.294, alpha: 1.0)
                }

                //set chosen color to button and round it up 
                aButton.backgroundColor = choosedColor

                aButton.setContactName(contactName: contacts[i])

                buttons.append(aButton)

            }

            horizontalScrollView.addItems(buttons)
            var x = horizontalScrollView.calculateMarginBetweenItems()


            cell.contentView.addSubview(horizontalScrollView)
            horizontalScrollView.translatesAutoresizingMaskIntoConstraints = false
            cell.contentView.addConstraint(NSLayoutConstraint.init(item: horizontalScrollView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: cell.frame.height))

            cell.contentView.addConstraint(NSLayoutConstraint.init(item: horizontalScrollView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem:cell.contentView, attribute: .width, multiplier: 1, constant: 0))




        }
        else{
            //cell must be message cell


        }

     return cell
     }

1 个答案:

答案 0 :(得分:0)

使用框架(等于故事板)重新调整表格视图正确配置所有元素