ios - 在 UIButton 中添加右 View

标签 ios swift swift3 uibutton uiimage

我试图在按钮上显示一些文本和图像。我正在使用 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)  

我可以在正确的位置看到图像,但是没有出现文本。我认为 titleEdgeInsets 不工作。

最佳答案

我试过的代码得到了你遇到的问题的输出。

btnSort.backgroundColor = UIColor.redColor() --> 设置背景色并勾选

 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)

并将 Action 处理为

  func showSortTbl() {
    // do your stuff here

}

输出

enter image description here

关于ios - 在 UIButton 中添加右 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33033737/

相关文章:

swift - NSManagedObject 的变化反射(reflect)了动态变化

ios - 为什么@nonescaping 闭包不需要 [weak self]?

ios - 如何从主视图 Controller 访问 ScrollView 子类(初学者)

xcode - 错误: "Cannot assign to immutable expression of type ' Bool'"?

iphone - 如何在 objective-c 中声明常量

iphone - 自定义 uitableviewcell 中的导航

ios - 我做了一个简单的更新,添加了两种声音,现在一些用户报告说,当您继续使用该应用时,它会立即退出

ios - SwiftUI - 如何关闭工作 TableView ,同时关闭该 View

iphone - 如何删除 UIWindow?