add rightview in UIButton

2020-02-08 18:12发布

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.

4条回答
成全新的幸福
2楼-- · 2020-02-08 18:23

Please check below code.

Hope it will work for you.

let teamImage: UIButton = UIButton(frame: CGRect(x: 0, y: 75, width: 100, height: 50))
let imageTest = UIImage(named: "immgg")

teamImage.setTitle("HypnotoadTitle", forState: .Normal)
teamImage.setBackgroundImage(imageTest, forState: .Normal)
self.view.addSubview(teamImage)
查看更多
姐就是有狂的资本
3楼-- · 2020-02-08 18:32

Subclass UIButton

Override the layoutSubviews() function

class CustomButton: UIButton {

    override func layoutSubviews() {
        super.layoutSubviews()

        guard imageView != nil else {
         return
        }

        imageEdgeInsets = UIEdgeInsets(top: 5, left: (bounds.width - 25), bottom: 5, right: 5)
        titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: imageView!.frame.width)

     }
}
  • Add a button to Storyboard
  • Add custom class to the button
  • Change button type to System
  • Set Title and image to the button
查看更多
Lonely孤独者°
4楼-- · 2020-02-08 18:37

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

enter image description here

查看更多
迷人小祖宗
5楼-- · 2020-02-08 18:40

Set only background color and display your text :

let btnSort = UIButton(type: UIButtonType.System) as UIButton // Shift 2.0

btnSort.backgroundColor = UIColor.yellowColor()

OR

btnSort.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
查看更多
登录 后发表回答