我正在以编程方式创建 UIButton ,并将标题,图片设置为以下代码。问题是每当我点击按钮时,图像会突出显示 ,但按钮的标题却没有。所以,我想创建两者或没有动画效果。我是否应该给出约束或什么使图像正确“SORT”?
答案 0 :(得分:1)
我钉了它。我将按钮类型更改为“系统”按钮,颜色变为蓝色。然而,我设法通过将色调颜色设置为白色来将蓝色更改为白色,苹果默认使用该颜色将颜色设置为其控件(我猜)。因此系统按钮颜色为蓝色是由于tintcolor被指定为蓝色
的原因let btnSort = UIButton.buttonWithType(UIButtonType.System) as UIButton
btnSort.frame = CGRectMake(2, 74, 140, 26)
btnSort.tintColor = UIColor.whiteColor()
btnSort.setImage(UIImage(named:"drop_arrow"), forState: UIControlState.Normal)
btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: 100,bottom: 6,right: 14)
btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: -30,bottom: 0,right: 34)
btnSort.setTitle("SORT", forState: UIControlState.Normal)
btnSort.layer.borderWidth = 1.0
btnSort.layer.borderColor = UIColor.whiteColor().CGColor
btnSort.addTarget(self, action: Selector("showSortTbl"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btnSort)
答案 1 :(得分:0)
以下是示例代码,您可以尝试一下。我以编程方式创建了一个按钮,并在EdgeInsets
属性的帮助下分配了图像。
let btnDemo : UIButton = UIButton()
btnDemo.frame = CGRect(x: 5, y: 75, width: 65, height: 18)
btnDemo.setImage(UIImage(named:"drop_arrow_sample"), forState: UIControlState.Normal)
btnDemo.imageEdgeInsets = UIEdgeInsets(top: 6,left: 100,bottom: 6,right: 14)
btnDemo.titleEdgeInsets = UIEdgeInsets(top: 0,left: -30,bottom: 0,right: 34)
btnDemo.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnDemo.layer.cornerRadius = 2.0
btnDemo.titleLabel?.textColor = UIColor.whiteColor()
btnDemo.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnDemo.tag = index
btnDemo.addTarget(self, action: "SortTable:", forControlEvents: UIControlEvents.TouchUpInside)