Swift UIButton不显示带“的圆圈”;我";信息灯

Swift UIButton不显示带“的圆圈”;我";信息灯,swift,swift3,uibutton,programmatically-created,Swift,Swift3,Uibutton,Programmatically Created,出于某种原因,当我运行下面的函数以编程方式添加UIButton(我确实希望以编程方式而不是在界面生成器中添加UIButton)时,在.infoLight中带有“i”的iOS按钮标准圆没有显示出来—我正在将其添加到UIPageViewController。我确实在屏幕左下角有一个按钮,我已经将其设置为.backgroundColor=.lightGray,以验证它是否已添加到子视图中&是否正常工作(左图,下图)。对我所缺少的最基本的东西有什么想法吗?非常感谢 func configureAbout

出于某种原因,当我运行下面的函数以编程方式添加UIButton(我确实希望以编程方式而不是在界面生成器中添加UIButton)时,在.infoLight中带有“i”的iOS按钮标准圆没有显示出来—我正在将其添加到UIPageViewController。我确实在屏幕左下角有一个按钮,我已经将其设置为.backgroundColor=.lightGray,以验证它是否已添加到子视图中&是否正常工作(左图,下图)。对我所缺少的最基本的东西有什么想法吗?非常感谢

func configureAboutButton() {
    let aboutButtonHeight: CGFloat = 44
    let aboutButtonWidth: CGFloat = 44
    aboutButton = UIButton(type: .infoLight)
    aboutButton.tintColor = UIColor.red
    aboutButton = UIButton(frame: CGRect(x: 0, y: view.frame.height - aboutButtonHeight, width: aboutButtonWidth, height: aboutButtonHeight))
    aboutButton.backgroundColor = .lightGray
    aboutButton.addTarget(self, action: #selector(segueToAboutVC), for: .touchUpInside)
    view.addSubview(aboutButton)
}
奇怪的是,如果我在设置背景色的下方加上这一行,右边的图像就会显示出来(否则我会看到左边的图像)

aboutButton.setTitle(“X”,表示:。正常)

问题 使用
aboutButton=ui按钮(类型:.infoLight))
创建按钮后,使用
aboutButton=ui按钮(frame:CGRect(x:0,y:view.frame.height-aboutButtonHeight,width:aboutButtonWidth,height:aboutButtonHeight))再次创建按钮。
。这就是信息图标不显示的原因

解决方案 只需通过设置框架属性来设置按钮的框架aboutButton.frame=CGRect(x:0,y:view.frame.height-aboutButtonHeight,width:aboutButtonWidth,height:aboutButtonHeight)就可以删除
aboutButton=UIButton(框架:CGRect(x:0,y:view.frame.height-aboutButtonHeight,width:aboutButtonWidth,height:aboutButtonHeight))

暗示 更好的解决方案是使用其中一个。在下面的版本中,我使用布局锚来设置约束。例如,您可以找到自动布局的优点

func configureAboutButton() {
  aboutButton = UIButton(type: .infoLight)
  // need to set to false, to set the constraints programmatically
  aboutButton.translatesAutoresizingMaskIntoConstraints = false
  aboutButton.tintColor = UIColor.red
  aboutButton.backgroundColor = .lightGray
  aboutButton.addTarget(self, action: #selector(segueToAboutVC), for: .touchUpInside)
  view.addSubview(aboutButton)

  // set the constraints via Layout Anchors
  // x, y, w, h
  aboutButton.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  aboutButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  aboutButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  aboutButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
}

这非常有帮助。非常感谢!太好了,我能帮你真是太好了!
func configureAboutButton() {
  aboutButton = UIButton(type: .infoLight)
  // need to set to false, to set the constraints programmatically
  aboutButton.translatesAutoresizingMaskIntoConstraints = false
  aboutButton.tintColor = UIColor.red
  aboutButton.backgroundColor = .lightGray
  aboutButton.addTarget(self, action: #selector(segueToAboutVC), for: .touchUpInside)
  view.addSubview(aboutButton)

  // set the constraints via Layout Anchors
  // x, y, w, h
  aboutButton.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  aboutButton.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  aboutButton.widthAnchor.constraint(equalToConstant: 44).isActive = true
  aboutButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
}