啊 - 我以前见过这个。
我不知道为什么,但在 a 中使用 SF 符号UIImageView
will 改变图像视图的高度!
您可以轻松确认这一点:
- 在 Storyboard 中添加视图控制器
- add a
UIImageView
- 将内容模式设置为中心(并不重要)
- 约束宽度:60 高度:60 centerX 和 centerY
- 检查尺寸检查器 - 它会显示
60 x 60
Now:
- 使用下拉菜单将图像设置为“地球”
- 检查尺寸检查器 - 它会显示
60 x 59
Now:
- 使用下拉菜单将图像设置为“信封”
- 检查尺寸检查器 - 它会显示
60 x 56.5
没有明显的约束冲突...没有明显的reason为了这。
据我所知(除非 iOS 14 发生变化),我们需要将图像视图嵌入到UIView
...约束它 centerX 和 centerY ...设置该视图的背景颜色和角半径属性以使其变圆。
Edit——只是作为一个练习……
两个水平堆栈视图:
- 对齐方式:填充
- 分布:均等填充
- 间距:0
- 每个都被限制为
Width: 300
and Height: 100
- 每个装满3个
UIImageView
s
给我们 3100 x 100
方形图像视图。对于第二个堆栈,将每个图像视图设置为系统图像,使用UIImage.SymbolConfiguration(pointSize: 60, weight: .regular)
. Should还是给我们3100 x 100
正方形。
相反,这是结果:
这是产生该结果的代码:
class SystemImageTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stack1 = UIStackView()
stack1.axis = .horizontal
stack1.alignment = .fill
stack1.distribution = .fillEqually
let stack2 = UIStackView()
stack2.axis = .horizontal
stack2.alignment = .fill
stack2.distribution = .fillEqually
// add stack views to the view
[stack1, stack2].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
stack1.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
stack1.widthAnchor.constraint(equalToConstant: 300.0),
stack1.heightAnchor.constraint(equalToConstant: 100.0),
stack1.centerXAnchor.constraint(equalTo: g.centerXAnchor),
stack2.topAnchor.constraint(equalTo: stack1.bottomAnchor, constant: 20.0),
stack2.widthAnchor.constraint(equalToConstant: 300.0),
stack2.heightAnchor.constraint(equalToConstant: 100.0),
stack2.centerXAnchor.constraint(equalTo: g.centerXAnchor),
])
let colors: [UIColor] = [
UIColor(red: 0.75, green: 0.00, blue: 0.00, alpha: 1.0),
UIColor(red: 0.00, green: 0.75, blue: 0.00, alpha: 1.0),
UIColor(red: 0.00, green: 0.00, blue: 1.00, alpha: 1.0),
]
for c in colors {
let v = UIImageView()
v.backgroundColor = c
stack1.addArrangedSubview(v)
}
let configuration = UIImage.SymbolConfiguration(pointSize: 60, weight: .regular)
let names: [String] = [
"globe",
"bandage",
"envelope",
]
for (c, n) in zip(colors, names) {
let v = UIImageView()
v.backgroundColor = c
v.contentMode = .center
v.tintColor = .white
let img = UIImage(systemName: n, withConfiguration: configuration)
v.image = img
stack2.addArrangedSubview(v)
}
}
}
Edit 2- 只是为了它......
为一个实现几个类SystemImageView
and CircularSystemImageView
:
class SystemImageView: UIView {
override var contentMode: UIView.ContentMode {
didSet {
imageView.contentMode = contentMode
}
}
override var tintColor: UIColor! {
didSet {
imageView.tintColor = tintColor
}
}
var image: UIImage = UIImage() {
didSet {
imageView.image = image
}
}
let imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
clipsToBounds = true
addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: topAnchor),
imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
}
}
class CircularSystemImageView: SystemImageView {
override func layoutSubviews() {
super.layoutSubviews()
self.layer.masksToBounds = true
self.clipsToBounds = true
self.layer.cornerRadius = bounds.size.width * 0.5
}
}
class SystemImageTestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stack1 = UIStackView()
stack1.axis = .horizontal
stack1.alignment = .fill
stack1.distribution = .fillEqually
let stack2 = UIStackView()
stack2.axis = .horizontal
stack2.alignment = .fill
stack2.distribution = .fillEqually
let stack3 = UIStackView()
stack3.axis = .horizontal
stack3.alignment = .fill
stack3.distribution = .fillEqually
let stack4 = UIStackView()
stack4.axis = .horizontal
stack4.alignment = .fill
stack4.distribution = .fillEqually
// add stack views to the view
[stack1, stack2, stack3, stack4].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
stack1.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
stack1.widthAnchor.constraint(equalToConstant: 300.0),
stack1.heightAnchor.constraint(equalToConstant: 100.0),
stack1.centerXAnchor.constraint(equalTo: g.centerXAnchor),
stack2.topAnchor.constraint(equalTo: stack1.bottomAnchor, constant: 20.0),
stack2.widthAnchor.constraint(equalToConstant: 300.0),
stack2.heightAnchor.constraint(equalToConstant: 100.0),
stack2.centerXAnchor.constraint(equalTo: g.centerXAnchor),
stack3.topAnchor.constraint(equalTo: stack2.bottomAnchor, constant: 20.0),
stack3.widthAnchor.constraint(equalToConstant: 300.0),
stack3.heightAnchor.constraint(equalToConstant: 100.0),
stack3.centerXAnchor.constraint(equalTo: g.centerXAnchor),
stack4.topAnchor.constraint(equalTo: stack3.bottomAnchor, constant: 20.0),
stack4.widthAnchor.constraint(equalToConstant: 300.0),
stack4.heightAnchor.constraint(equalToConstant: 100.0),
stack4.centerXAnchor.constraint(equalTo: g.centerXAnchor),
])
let colors: [UIColor] = [
UIColor(red: 0.75, green: 0.00, blue: 0.00, alpha: 1.0),
UIColor(red: 0.00, green: 0.75, blue: 0.00, alpha: 1.0),
UIColor(red: 0.00, green: 0.00, blue: 1.00, alpha: 1.0),
]
for c in colors {
let v = UIImageView()
v.backgroundColor = c
stack1.addArrangedSubview(v)
}
let configuration = UIImage.SymbolConfiguration(pointSize: 60, weight: .regular)
let names: [String] = [
"globe",
"bandage",
"envelope",
]
for (c, n) in zip(colors, names) {
let v = UIImageView()
v.backgroundColor = c
v.contentMode = .center
v.tintColor = .white
if let img = UIImage(systemName: n, withConfiguration: configuration) {
v.image = img
}
stack2.addArrangedSubview(v)
}
for (c, n) in zip(colors, names) {
let v = SystemImageView()
v.backgroundColor = c
v.contentMode = .center
v.tintColor = .white
if let img = UIImage(systemName: n, withConfiguration: configuration) {
v.image = img
}
stack3.addArrangedSubview(v)
}
for (c, n) in zip(colors, names) {
let v = CircularSystemImageView()
v.backgroundColor = c
v.contentMode = .center
v.tintColor = .white
if let img = UIImage(systemName: n, withConfiguration: configuration) {
v.image = img
}
stack4.addArrangedSubview(v)
}
}
}
Results:
或者,更实用一点let configuration = UIImage.SymbolConfiguration(pointSize: 40, weight: .regular)
显示尺寸不是由 SF 符号尺寸决定: