当前位置: 首页 > 面试题库 >

在UIButton中添加rightview

公冶伟
2023-03-14
问题内容

我试图在按钮上显示一些文本和图像。我正在从这里使用代码

    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没有用。


问题答案:

我尝试的代码获得了输出U所遇到的问题。

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)

and handle the action as

  func showSortTbl() {
    // do your stuff here

}


 类似资料:
  • 问题内容: 我有一个通用的控件类,需要根据视图控制器来设置按钮的完成方式。由于setLeftButtonActionWithClosure函数需要将一个闭包作为参数来设置,因此应该将其设置为取消按钮的动作。在Swift中怎么可能因为我们需要将函数名称作为String传递给action:参数。 问题答案: 注意:就像@EthanHuang一样,“如果您有两个以上的实例,则此解决方案将不起作用。所有操

  • UIButton-Bootstrap 是 UIButton 的分类,实现了 Bootstrap 3.0 风格的按钮效果。无需任何图片,纯代码实现(Quartz Core)具有扁平化、Bootstrap风格的按钮。 [Code4App.com]

  • 问题内容: 我正在尝试使用Swift更改UIButton的图像…我该怎么办 这是OBJ-C代码。但是我不知道Swift: 问题答案: 从您的Obc-C代码中,我认为您想为按钮设置图片,因此请尝试以下方式: 简而言之: 对于Swift 3:

  • 我创建了一个表单,其中添加了一个,它有3列。第二列和第三列有编辑器。 我希望当我们选择第二列组合框的第一项时,第三列组合框的第一个组合框也应该被选择,反之亦然。 我该怎么做?

  • 问题内容: 我知道我可以使用以下方法绕过所有四个角: 因为我只是想第二轮中,我做了一些研究,发现这个: 如下所示: 但是,这不适用于UIButton。当我将扩展名切换为UIButton类型并将其传递给button时,输出如下所示: 问题是,如何使它适应于UIButton? 问题答案: 添加UIButton扩展: 在viewDidAppear / viewDidLayoutSubviews中调用:

  • 问题内容: 我正在尝试以编程方式构建UI。如何使该动作起作用?我正在用Swift开发。 viewDidLoad中的代码: 问题答案: 您只是在选择器名称的末尾缺少冒号。因为按下需要一个参数,所以冒号必须在该位置。而且,您按下的函数不应嵌套在viewDidLoad中。 编辑:更新以反映Swift 2.2中的最佳实践。应该使用#selector()而不是不推荐使用的文字字符串。