Upputton以编程方式创建,在按下时不会改变颜色,就像使用Interface Builder创建的所有其他按钮一样。
我的按钮创建如下:
let cableDetailsButton = UIButton(type: UIButtonType.System)
cableDetailsButton.frame = CGRectMake(8, 8, 42, 21)
cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
cableDetailsButton.addTarget(self, action: #selector(FuseDetailsViewController.cableButtonPressed), forControlEvents: .TouchUpInside)
我想补充一点,tintColor是默认的iOS颜色(蓝色)
我的目标是以编程方式创建相同的(默认!)按钮,因为从Interaface Builder中的列表中拖放一个按钮。我希望我的按钮在按下时改变颜色/ alpha。
我正在使用Swift 2.3
不幸的是,这个简单的任务让我不知所措,以至于我要求帮助。提前感谢您的帮助。
更新:按钮是UISCrollView的一部分
答案 0 :(得分:3)
而不是
new_hash = hash1.map { |k, v| [k, v.to_i] }.to_h
new_hash == hash2 ? 1 : 0
=> 1
您需要将let cableDetailsButton = UIButton(type: UIButtonType.system)
更改为UIButtonType
.custom
如果您正在使用Swift3,语法也是错误的。找到下面更正的语法
let cableDetailsButton = UIButton(type: UIButtonType.custom)
答案 1 :(得分:0)
这对我有用。
let cableDetailsButton = UIButton(type: .Custom)
cableDetailsButton.frame = CGRect(x: 68, y: 68, width: 100, height: 70)
cableDetailsButton.setTitle("Some button", forState: .Normal)
cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
view.addSubview(cableDetailsButton)
或者我并不完全明白你的问题是什么。
答案 2 :(得分:0)
有两种方法可以突出显示按钮
方法1
let cableDetailsButton = UIButton(type: UIButtonType.System)
cableDetailsButton.frame = CGRectMake(20, 50, self.view.frame.size.width - 32, 50)
cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
cableDetailsButton.addTarget(self, action: #selector(cableButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
cableDetailsButton.backgroundColor = UIColor.whiteColor()
self.view.addSubview(cableDetailsButton)
func cableButtonPressed(sender: UIButton){
dispatch_async(dispatch_get_main_queue(), {
if self.isHighLighted == false{
sender.highlighted = true;
self.isHighLighted = true
}else{
sender.highlighted = false;
self.isHighLighted = false
}
});
}
方法2
let cableDetailsButton = UIButton(type: UIButtonType.System)
cableDetailsButton.frame = CGRectMake(20, 50, self.view.frame.size.width - 32, 50)
cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
cableDetailsButton.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
cableDetailsButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
cableDetailsButton.addTarget(self, action: #selector(cableButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(cableDetailsButton)
func cableButtonPressed(sender: UIButton){
if sender.selected {
// set selected
sender.selected = true
} else {
// set deselected
sender.selected = false
}
}
答案 3 :(得分:0)
在视图控制器中实现标记
var buttonSelected = false
保留按钮的代码,但删除一行
let cableDetailsButton = UIButton(type: UIButtonType.System)
cableDetailsButton.frame = CGRectMake(8, 8, 42, 21)
cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
cableDetailsButton.addTarget(self, action: #selector(FuseDetailsViewController.cableButtonPressed), forControlEvents: .TouchUpInside)
添加以下内容
func cableButtonPressed() {
if buttonSelected {
cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
buttonSelected = !buttonSelected
//do whatever else you need to do
}else {
cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
buttonSelected = !buttonSelected
//do whatever else you need to do
}
//do whatever else you need to do
}