【问题标题】:add rightview in UIButton在 UIButton 中添加右视图
【发布时间】:2016-01-07 03:14:27
【问题描述】:

我正在尝试在按钮上显示一些文本和图像。我正在使用来自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 不起作用。

【问题讨论】:

    标签: ios swift swift3 uibutton uiimage


    【解决方案1】:

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

    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)
    

    并将动作处理为

      func showSortTbl() {
        // do your stuff here
    
    }
    

    输出

    【讨论】:

    • 说真的,我不知道问题是什么,但我仍然没有得到它。我用红色检查过。我使用的是 swift 1.2 是否有问题?
    • @Saty -- 我只是添加了按钮的背景颜色并检查了我也显示了答案,你尝试了更新的答案
    • 当我删除图像时,我看到了文字,我正在使用 UIButton.buttonWithType(UIButtonType.System) 作为! UIButton,因为我正在使用 swift 1.2。我认为这是导致问题的原因
    • 不不不,兄弟不喜欢那样,好的,在这个地方尝试一次 UIButton.buttonWithType(UIButtonType.System) 使用 UIButton.buttonWithType(UIButtonType.Custom) 和 btnSort.frame = CGRectMake(2, 74, 140 , 40) 尝试一次
    • 对不起兄弟!!!你是绝对正确的!!!这是因为我使用的是 200X200 大小的图像。这就是为什么不工作!!!!
    【解决方案2】:

    子类UIButton

    覆盖layoutSubviews() 函数

    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)
    
         }
    }
    
    • 向情节提要添加按钮
    • 为按钮添加自定义类
    • 将按钮类型更改为系统
    • 为按钮设置标题和图片

    【讨论】:

      【解决方案3】:

      请检查以下代码。

      希望它对你有用。

      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)
      

      【讨论】:

        【解决方案4】:

        仅设置背景颜色并显示您的文字:

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

        或

        btnSort.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
        

        【讨论】:

          猜你喜欢
          • 2013-11-11
          • 1970-01-01
          • 1970-01-01
          • 2011-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多