Ios 尝试使用图像和文本创建UIButton时得到奇怪的结果

Ios 尝试使用图像和文本创建UIButton时得到奇怪的结果,ios,swift,image,text,uibutton,Ios,Swift,Image,Text,Uibutton,我正在尝试创建一个带有自定义UIButtonType的UIButtonType,配置为左对齐图像和居中文本。然而,我能找到的每一个例子(有很多)都不起作用,产生了一些奇怪的结果。 我使用的代码是: let button = UIButton(type: UIButtonType.custom) button.frame = CGRect(x: 50, y: 50, width: 100, height: 100) button.addTarget(self, action: #selector(

我正在尝试创建一个带有自定义
UIButtonType
UIButtonType
,配置为左对齐图像和居中文本。然而,我能找到的每一个例子(有很多)都不起作用,产生了一些奇怪的结果。 我使用的代码是:

let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
button.addTarget(self, action: #selector(StartController.button), for: .touchUpInside)
button.setImage(UIImage(named: "IMG_0124"), for: .normal)
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
button.setTitle("Back", for: .normal)
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0)
button.contentHorizontalAlignment = .left
我在调试器中得到以下信息:

(lldb) po button.imageView?.frame
▿ Optional<CGRect>
  ▿ some : (10.0, 0.0, 90.0, 100.0)
    ▿ origin : (10.0, 0.0)
      - x : 10.0
      - y : 0.0
    ▿ size : (90.0, 100.0)
      - width : 90.0
      - height : 100.0


(lldb) po button.titleLabel?.frame
▿ Optional<CGRect>
  ▿ some : (100.0, 50.0, 2368.0, 0.0)
    ▿ origin : (100.0, 50.0)
      - x : 100.0
      - y : 50.0
    ▿ size : (2368.0, 0.0)
      - width : 2368.0
      - height : 0.0 
(lldb)po按钮.imageView?框架
▿ 可选的
▿ 一些:(10.0,0.0,90.0,100.0)
▿ 来源:(10.0,0.0)
-x:10.0
-y:0.0
▿ 尺寸:(90.0、100.0)
-宽度:90.0
-身高:100.0
(lldb)po按钮。标题标签?框架
▿ 可选的
▿ 一些:(100.0,50.0,2368.0,0.0)
▿ 原产地:(100.0,50.0)
-x:100.0
-y:50.0
▿ 尺寸:(2368.0,0.0)
-宽度:2368.0
-高度:0.0
我无法通过
按钮.imageView?.frame
设置帧

这就是我想要的:

这就是我得到的:

该按钮不是UINavigationController的一部分。它是简单UIView的子视图

知道怎么回事吗?

swift 3. 试试这个


可能有几件事不对劲,但除非你告诉我们两件事,否则很难说。(1) 你希望看到什么?还有,这应该是UINavigationBar的一部分吗?我这么问是因为“背”的标题。(2) 你在看什么?任何东西帮助我们帮助你。对不起,那没用
button.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
button.titleLabel.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
button.imageView.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)`
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)