在 UIButton 中添加右视图 [英] add rightview in UIButton

查看:18
本文介绍了在 UIButton 中添加右视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在按钮上显示一些文本和图像.我正在使用

I am trying to display some text and an image over a button. I am using the code from here

    let btnSort   = UIButton.buttonWithType(UIButtonType.System) as! UIButton
    btnSort.frame =  CGRectMake(2, 74, 140, 26)
    btnSort.tintColor = UIColor.whiteColor()
    btnSort.setImage(UIImage(named:"immgg"), 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)  

I can see the image at the right place, however the text is not appearing. I think titleEdgeInsets is not working.

解决方案

the code I tried I got the output what the problem U faced.

btnSort.backgroundColor = UIColor.redColor() --> set the background color and check

 let btnSort   = UIButton(type: UIButtonType.System) as UIButton! //this is Swift2.0 in this place use your code
    btnSort.frame =  CGRectMake(2, 74, 140, 40)
    btnSort.tintColor = UIColor.whiteColor()
    btnSort.setImage(UIImage(named:"youtube16x16.png"), 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.backgroundColor = UIColor.redColor() --> set the background color and check 
    btnSort.layer.borderColor = UIColor.whiteColor().CGColor
    btnSort.addTarget(self, action: Selector("showSortTbl"), forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(btnSort)

Swift3

 let btnSort   = UIButton(type: .system)
    btnSort.frame =  CGRect(x: 2, y: 74, width: 140, height: 40)
    btnSort.tintColor = UIColor.white
    btnSort.setImage(UIImage(named:"youtube16x16.png"), for: .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", for: .normal)
    btnSort.layer.borderWidth = 1.0
    btnSort.backgroundColor = UIColor.red //--> set the background color and check
    btnSort.layer.borderColor = UIColor.white.cgColor
    btnSort.addTarget(self, action: #selector(ViewController.showSortTbl), for: UIControlEvents.touchUpInside)
    self.view.addSubview(btnSort)

and handle the action as

  func showSortTbl() {
    // do your stuff here

}

output

这篇关于在 UIButton 中添加右视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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