Ich versuche eine ASHorizontalScrollView zu implementieren (genau wie das horizontale Scroll-Menü von App Strore). Ich stelle den Rahmen ein, aber das letzte Element wird nie angezeigt, seine Größe sieht größer aus als die Superansicht. Sie können das letzte Element sehen, das es dort ist, aber es kann es nie bringen.ASHorizontalScrollView zeigt nicht alle Elemente an
konfiguriert I die Größe der horizontalen Scroll wie die Größe des Tablecell zugeordnet ist.
Meine Frage ist, wie setze ich HORIZONTAL SCROLLVIEW Breite, um alle Elemente anzuzeigen?
dies ist, wie ich es am CellForRowAtIndexPath gesetzt:
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
}