Ich habe eine Liste der Kategorie hinzufügen Ich habe Schaltflächen für jede Kategorie in einer Bildlaufansicht erstellt und eine letzte Schaltfläche ist die Schaltfläche für alle Produkte. Aber einige, wie es keine Aktion hatte, fügte ich hinzu. dies ist mein Code Zuerst habe ich eine Funktion machen Schaltflächen erstellen hinzuzufügenLetzte Schaltfläche in UIScrollView Ziel hinzufügen wirkt nicht
func catButtonView(buttonSize:CGSize) -> UIView {
let buttonView = UIView()
buttonView.backgroundColor = UIColor(colorLiteralRed: 1, green: 1, blue: 1, alpha: 0)
buttonView.frame.origin = CGPoint(x: 0,y: 0)
let padding = CGSize(width:0, height:0)
buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(categories.count)
buttonView.frame.size.height = (buttonSize.height + 2.0 * padding.height)
//add buttons to the view
var buttonPosition = CGPoint(x:padding.width * 0.5, y:padding.height)
let buttonIncrement = buttonSize.width + padding.width
let hueIncrement = 1.0/CGFloat(categories.count)
var newHue = hueIncrement
for i in 0...(categories.count) {
let button = UIButton.init(type: .custom) as UIButton
button.frame.size = buttonSize
button.frame.origin = buttonPosition
button.setBackgroundImage(R.image.button(), for: .normal)
button.setBackgroundImage(R.image.button_selected(), for: .selected)
if(i==categories.count) {
button.setTitle("ALL", for: .normal)
} else {
button.setTitle(categories[i].name, for: .normal)
}
button.setTitleColor(UIColor.white, for: .normal)
buttonPosition.x = buttonPosition.x + buttonIncrement
newHue = newHue + hueIncrement
button.tag = i+1
button.addTarget(self, action: #selector(catButtonPressed(sender:)), for: .touchUpInside)
buttonView.addSubview(button)
}
return buttonView
}
diese Aktion func
func catButtonPressed(sender:UIButton){
print(sender.tag)
if(self.selectedCat != sender.tag-1) {
let lastBtn = catScrollView.viewWithTag(selectedCat+1) as? UIButton
lastBtn?.setBackgroundImage(R.image.button(), for: .normal)
self.selectedCat = sender.tag-1
self.itemsCollection.reloadData()
sender.setBackgroundImage(R.image.button_selected(), for: .normal)
}
}
fügen Sie dann blättern Ansicht wissen
let catScrollingView = catButtonView(buttonSize: CGSize(width: 200.0, height:50.0))
catScrollView.contentSize = catScrollingView.frame.size
catScrollView.subviews.forEach({ $0.removeFromSuperview() })
catScrollView.addSubview(catScrollingView)
catScrollView.showsHorizontalScrollIndicator = true
catScrollView.indicatorStyle = .default
self.itemsCollection.reloadData()
Jedermann zu sehen was ich vermisst habe?
überprüfen Sie Ihre Ansicht blättern Höhe und Ihre Tastenposition und eine Zeile ändern, dass button.addTarget (self, action: #selector (catButtonPressed (sender :)), für: .touchDown) und check output –
Danke, ich glaube ich kenne das Problem, die Frame-Breite nur für categories.count, aber nicht categories.count +1 wo "ALL" button sind – Thanh
ya thats ich möchte Ihnen sagen, dass Ihre Schaltflächen nicht sichtbar sind, also fügen Sie es in Sicht –