我正在使用表格视图和给定的约束(顶部,letf,右边,底部),并且它的打印宽度在所有设备中都相同,但是所有设备的单元格宽度都不同。 但问题是我已经在单元格中查看并再次给出约束(顶部,左右,底部),并且在所有设备中打印时的视图宽度相同。怎么可能?我的手机宽度不同,其内容视图在所有设备中都有相同的宽度? 在遇到限制时缺少什么,请向我解释一下?
答案 0 :(得分:1)
答案 1 :(得分:0)
带有红色背景的视图被赋予约束,并且在故事板上标记了自动调整子视图。
覆盖func awakeFromNib(){ super.awakeFromNib()
//Corner Radius to cell contnt view
view_ContentView.layer.cornerRadius = 10.0
view_ContentView.layer.borderColor = UIColor.lightGray.cgColor
view_ContentView.layer.borderWidth = 1.5
view_ContentView.clipsToBounds = true
view_SubServices.backgroundColor = UIColor.red
self.CreateDynamicLabelOnView(px: 2.0, view: view_SubServices, array: ["Hair Trimming" ,"Hair Color","Dry Cleaning","Boutique","Washing"],textColor:UIColor.white,backgroundColor: Common.k_Purple)
}
func CreateDynamicLabelOnView(px:CGFloat,view:UIView,array:NSMutableArray,textColor:UIColor,backgroundColor:UIColor){
var px:CGFloat = 2.0
var tx:CGFloat?
array.enumerateObjects({ (obj, idx, stop) in
var width = (obj as! String).widthOfString(usingFont: UIFont(name: "Times New Roman", size: 13.0)!)
if width < 30.0{
width = 45.0
}
print(width)
print(px)
print(view.frame.size.width)
tx = px + width
print(tx!)
// if width < view.frame.size.width && tx! < view.frame.size.width {
if tx! < view.frame.size.width {
let label = UILabel(frame:CGRect(x: px, y: 2.0, width: width , height: 25.0))
label.backgroundColor = backgroundColor
label.textColor = textColor
label.textAlignment = .center
label.font = UIFont(name: "Times New Roman", size: 12.0)
label.layer.cornerRadius = 9.0
label.text = array[idx] as? String
label.adjustsFontSizeToFitWidth = true
label.clipsToBounds = true
view.addSubview(label)
px = px + label.frame.size.width + 5
}
else{
stop.pointee = true
}
})
}