Ios 如何以编程方式为en和ar语言环境布局UIImageView,并在其上添加文本?
我正在尝试布局图像视图,标签就在它旁边。我得到了在英语语言环境下工作的布局,但它给出了以下错误约束。它无法在阿拉伯语模式下渲染,Ios 如何以编程方式为en和ar语言环境布局UIImageView,并在其上添加文本?,ios,swift,uiimageview,nslayoutconstraint,Ios,Swift,Uiimageview,Nslayoutconstraint,我正在尝试布局图像视图,标签就在它旁边。我得到了在英语语言环境下工作的布局,但它给出了以下错误约束。它无法在阿拉伯语模式下渲染,UIImageView未将其位置切换到右侧 英语区域的屏幕截图 然后在stackView中添加profileHeaderView。如何解决这个问题?在这里使用嵌套的UIStackViews会更容易,这样约束问题就不会那么严重了。你想要的是 .____________________________. |.________. ._______________.| ||
UIImageView
未将其位置切换到右侧
英语区域的屏幕截图
然后在stackView中添加
profileHeaderView
。如何解决这个问题?在这里使用嵌套的UIStackView
s会更容易,这样约束问题就不会那么严重了。你想要的是
.____________________________.
|.________. ._______________.|
|| | |._____________.||
|| | ||Welcome |||
|| | |'-------------'||
|| | |._____________.||
|| | ||username |||
|| | |'-------------'||
|'--------' '---------------'|
'----------------------------'
如果标签位于垂直视图中,则堆栈视图和图像位于水平视图中
在运行时,当用户切换到从右向左的区域设置时,删除图像并将其重新插入外部堆栈视图的末尾。从左向右切换时,请执行相反的操作
此处需要的唯一约束是图像的尺寸和最外层堆栈视图相对于其父对象的位置。堆栈视图的间距和对齐属性将替代嵌套项的约束
// UI.swift
static func imageView(_ imageName: String) -> UIImageView {
let iv = UIImageView(image: UIImage(named: imageName))
iv.translatesAutoresizingMaskIntoConstraints = true
iv.clipsToBounds = true
return iv
}
// ProfileViewController.swift
lazy var profileImageView: UIImageView = {
let iv = UI.imageView("profile-placeholder")
return iv
}()
lazy var welcomeLbl: UILabel = {
let lbl = UI.label(text: UI.lmsg("Welcome"))
lbl.font = UIFont(name: "Roboto", size: 16)
return lbl
}()
lazy var nameLbl: UILabel = {
let lbl = UI.label(text: "username placeholder")
lbl.font = UIFont(name: "Roboto-Bold", size: 16)
return lbl
}()
lazy var profileHeaderView: UIView = {
let v = UI.view()
v.translatesAutoresizingMaskIntoConstraints = false
let lblView = UI.view()
lblView.translatesAutoresizingMaskIntoConstraints = false
lblView.addSubview(welcomeLbl)
lblView.addSubview(nameLbl)
lblView.layer.borderColor = UIColor.red.cgColor
lblView.layer.borderWidth = 1.0
// label view
NSLayoutConstraint.activate([
welcomeLbl.topAnchor.constraint(equalTo: lblView.topAnchor, constant: 0),
welcomeLbl.leadingAnchor.constraint(equalTo: lblView.leadingAnchor, constant: 0),
welcomeLbl.trailingAnchor.constraint(equalTo: lblView.trailingAnchor, constant: 0),
welcomeLbl.bottomAnchor.constraint(equalTo: nameLbl.topAnchor, constant: -8)
])
NSLayoutConstraint.activate([
nameLbl.topAnchor.constraint(equalTo: welcomeLbl.bottomAnchor, constant: 8),
nameLbl.leadingAnchor.constraint(equalTo: lblView.leadingAnchor, constant: 0),
nameLbl.trailingAnchor.constraint(equalTo: lblView.trailingAnchor, constant: 0),
nameLbl.bottomAnchor.constraint(equalTo: lblView.bottomAnchor, constant: 0)
])
v.addSubview(profileImageView)
v.addSubview(lblView)
NSLayoutConstraint.activate([
profileImageView.centerYAnchor.constraint(equalTo: v.centerYAnchor, constant: 0),
profileImageView.leadingAnchor.constraint(equalTo: v.leadingAnchor, constant: 0),
profileImageView.trailingAnchor.constraint(equalTo: lblView.leadingAnchor, constant: -8),
profileImageView.widthAnchor.constraint(equalToConstant: 38)
])
NSLayoutConstraint.activate([
lblView.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor, constant: 0),
lblView.leadingAnchor.constraint(equalTo: profileImageView.trailingAnchor, constant: 8),
lblView.trailingAnchor.constraint(equalTo: v.trailingAnchor, constant: -8)
])
v.layer.borderColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
v.layer.borderWidth = 1.0
return v
}()
.____________________________.
|.________. ._______________.|
|| | |._____________.||
|| | ||Welcome |||
|| | |'-------------'||
|| | |._____________.||
|| | ||username |||
|| | |'-------------'||
|'--------' '---------------'|
'----------------------------'