2017-02-26 95 views
0

我想实现一个ASHorizo​​ntalScrollView(就像App Strore水平滚动菜单)。我正在调整它的框架,但最后一个元素从不显示,它的尺寸看起来比超级视图更大。你可以看到它的最后一个元素,但永远不会启用它。ASHorizo​​ntalScrollView不显示所有元素

I touch and drag to force to show the last element

我配置了水平滚动视图的大小,因为它是分配的TableCell的大小。

我的问题是如何设置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 
    } 

回答

0

正确

重新调整与帧(等于故事板)表视图配置的所有元素