如何在按钮上显示图像?-迅速 [英] How to Display Image over Button? -Swift

查看:40
本文介绍了如何在按钮上显示图像?-迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式创建一个 UIButton 并将标题、图像设置为以下代码.问题是每当我点击按钮时,图像就会突出显示,但按钮的标题却没有.所以,我想创建两者或都不创建的动画效果.以及我应该给出限制还是什么以使图像正确地显示文本SORT"?

I am creating a UIButton programmatically and set the title, image as the below code. The problem is whenever i click on the button the image gets highlighted but the title of the button does not. So, i want to create the animation effect of both or none. As well as should i give constraints or what so that image gets right of the text "SORT"?

推荐答案

我搞定了.我将按钮类型更改为系统按钮,颜色更改为蓝色.但是,我设法通过将色调颜色设置为白色来将蓝色更改为白色,苹果默认使用白色将颜色设置为其控件(我猜).所以系统按钮颜色为蓝色是由于 tintcolor 被指定为蓝色的原因

I nailed it. I changed the button type to System button and the color changed to blue. However i managed to change the Blue color to white by setting the tint color to white which apple uses by default to set the color to its controls(i guess). So the system button color to blue was due to the reason the tintcolor was assigned as blue

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)

这篇关于如何在按钮上显示图像?-迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆