我已编码此代码:
let info_centill: UITextView = {
let tv = UITextView()
tv.text = "Connecting people with similar hobbies and interests around the world!"
tv.font = UIFont(name: "ZonaPro", size: 20)
tv.textColor = .lightGray
tv.textAlignment = .center
tv.frame = CGRect(x:0,y:30,width:0,height:0)
tv.backgroundColor = .gray
let fixedWidth = tv.frame.size.width
tv.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let newSize = tv.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
var newFrame = tv.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
tv.frame = newFrame
return tv
}()
info_centill.layoutIfNeeded()
info_centill.topAnchor.constraint(equalTo: view.topAnchor,constant: 120).isActive = true
info_centill.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
info_centill.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
当我运行此代码时,我在模拟器中得到了这个结果:
但在iPhone上我只能得到没有文字的灰色背景。问题是什么?我应该相信哪一个?