UIButton 没有为 .infoLight 显示带有“i”的圆圈
Posted
技术标签:
【中文标题】UIButton 没有为 .infoLight 显示带有“i”的圆圈【英文标题】:UIButton not showing the circle with "i" for .infoLight 【发布时间】:2017-02-19 18:21:05 【问题描述】:由于某种原因,当我运行下面的函数以编程方式添加 UIButton 时,没有显示带有“i”的 .infoLight 的 ios 按钮标准圆圈(我确实想以编程方式而不是在界面中执行此操作builder) - 我将它添加到 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", for: .normal)
【问题讨论】:
【参考方案1】:问题
使用aboutButton = UIButton(type: .infoLight))
创建按钮后,使用aboutButton = UIButton(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(frame: CGRect(x: 0, y: view.frame.height - aboutButtonHeight, width: aboutButtonWidth, height: aboutButtonHeight))
:
func configureAboutButton()
let aboutButtonHeight: CGFloat = 44
let aboutButtonWidth: CGFloat = 44
aboutButton = UIButton(type: .infoLight)
aboutButton.tintColor = UIColor.red
aboutButton.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)
提示
更好的解决方案是使用auto layout options to set constraints programmatically 之一。在下面的版本中,我使用 Layout Anchors 来设置约束。自动布局的优点你可以找到 here 的例子。
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
【讨论】:
这非常有帮助。非常感谢! 完美,很高兴能帮到你!以上是关于UIButton 没有为 .infoLight 显示带有“i”的圆圈的主要内容,如果未能解决你的问题,请参考以下文章