作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 CustomCollectionViewCell 类的 UICollectionView。在单元格内,我有一个用 Storyboard设置的按钮。我希望按钮在点击时改变其颜色,但我总是得到类似标题阴影的东西,其蓝色与选择时的背景颜色不同。
我尝试使用 setTitleShadowColor:ForState
以及 .Selected
和 .Normal
修复此问题。但它似乎没有做任何事情。
View Controller :
...
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfButtons
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCollectionViewCell
cell.setContents(indexPath.row)
return cell
}
...
自定义CollectionViewCell:
...
func setContents(row: Int) {
buttonInCell.setTitle(String(row + 1), forState: .Normal)
buttonInCell.setTitleColor(UIColor.blueColor(), forState: .Normal)
buttonInCell.layer.backgroundColor = UIColor.whiteColor().CGColor
buttonInCell.layer.cornerRadius = buttonRadius
buttonInCell.layer.borderColor = UIColor.blueColor().CGColor
buttonInCell.layer.borderWidth = buttonBorderWidth
}
func changeButtonStatus(sender: UIButton) {
if sender.selected {
sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
sender.layer.backgroundColor = UIColor.whiteColor().CGColor
} else {
sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
sender.layer.backgroundColor = UIColor.blueColor().CGColor
}
}
// MARK: Actions
@IBAction func buttonInCellTapped(sender: UIButton) {
changeButtonStatus(sender)
sender.selected = !sender.selected
}
...
最佳答案
我刚刚找到了一个解决方案,只需在 Xcode 的属性检查器中将按钮类型更改为“自定义”即可。
关于ios - UIButton `setTitleShadowColor:ForState` 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773765/
我有一个带有 CustomCollectionViewCell 类的 UICollectionView。在单元格内,我有一个用 Storyboard设置的按钮。我希望按钮在点击时改变其颜色,但我总是得
我是一名优秀的程序员,十分优秀!