我正在UIImageView中使用SF符号图像.我也给出了约束.我看到在不同的模拟器中运行相同的代码实际上增加了映像的宽度,但我想给不同的iOS模拟器提供固定的宽度.

以下是该图像的代码:

let rightImageView: UIImageView = {
    let rightImageView = UIImageView()
    rightImageView.image = UIImage(systemName: "arrow.right")
    rightImageView.translatesAutoresizingMaskIntoConstraints = false
    rightImageView.tintColor = .purple
    return rightImageView
}()

下面是对它和其他属性的约束:

private func setUpUI() {
    view.addSubview(stackview)
    stackview.dm_addArrangedSubviews(titleLable)
    stackview.dm_addArrangedSubviews(viewAllLable)
    stackview.dm_addArrangedSubviews(rightImageView)
    view.addSubview(collectionView)
    
    let safeArea = view.safeAreaLayoutGuide
    NSLayoutConstraint.activate([
        
        stackview.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            stackview.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            stackview.topAnchor.constraint(equalTo: view.topAnchor),
            collectionView.topAnchor.constraint(equalTo: stackview.bottomAnchor),
            collectionView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor),
            collectionView.heightAnchor.constraint(equalToConstant: view.frame.width/2)
    ])
}

Here is the screenshot in iPhone 15 Pro Max: 15th Pro max

Here is the screenshot in iPhone 15 simulator: 15

Here is the screenshot on different mode, the right arrow image width is set properly: different mode

如您所见,rightImageView属性的宽度增加了.我如何为不同的设备提供固定的宽度?

推荐答案

rightImageView.widthAnchor设置为等于rightImageView.heightAnchor.

将内容拥抱优先级设置为viewAllLable,这样它就不会水平伸展.

快速示例:

class ViewController: UIViewController {
    
    let rightImageView: UIImageView = {
        let rightImageView = UIImageView()
        rightImageView.image = UIImage(systemName: "arrow.right")
        rightImageView.tintColor = .purple
        return rightImageView
    }()
    
    let stackview: UIStackView = {
        let v = UIStackView()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.spacing = 8
        return v
    }()
    
    let titleLable: UILabel = {
        let v = UILabel()
        v.font = .systemFont(ofSize: 24.0, weight: .regular)
        v.text = "Similar Movies"
        return v
    }()

    let viewAllLable: UILabel = {
        let v = UILabel()
        v.font = .systemFont(ofSize: 24.0, weight: .regular)
        v.text = "View all"
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
    
    private func setUpUI() {
        view.addSubview(stackview)
        stackview.addArrangedSubview(titleLable)
        stackview.addArrangedSubview(viewAllLable)
        stackview.addArrangedSubview(rightImageView)
        
        let safeArea = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            
            // constrain to safeArea, with 8-point "padding" on top/leading/trailing
            stackview.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 8.0),
            stackview.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -8.0),
            stackview.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 8.0),
            
            rightImageView.widthAnchor.constraint(equalTo: rightImageView.heightAnchor),
            
        ])
        
        // don't let viewAllLable stretch horizontally
        viewAllLable.setContentHuggingPriority(.required, for: .horizontal)
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // toggle colors so we can see the framing
        if titleLable.backgroundColor == .yellow {
            titleLable.backgroundColor = .clear
            viewAllLable.backgroundColor = .clear
            rightImageView.backgroundColor = .clear
            
            stackview.layer.borderWidth = 0
        } else {
            titleLable.backgroundColor = .yellow
            viewAllLable.backgroundColor = .green
            rightImageView.backgroundColor = .cyan
            
            stackview.layer.borderColor = UIColor.red.cgColor
            stackview.layer.borderWidth = 1
        }
    }
}

如下所示:

enter image description here

无论视图宽度如何,元素宽度都保持不变:

enter image description here

当您运行上述代码时,点击任意位置以打开/关闭 colored颜色 ,以便您可以看到边框:

enter image description here

enter image description here

Ios相关问答推荐

ITMS—91053:Flutter Project中缺少API声明

在SWIFT中将圆角+透明背景从导入视频(.mp4)添加到导出视频(.mov)

带有RadStudio 11.3/12的Mac Mini M2(Sonoma 14.2.1)上的PAServer-测试连接按钮有效,但部署不起作用

JSON响应中的键有时为Int,有时为字符串

iOS 16.4,SwiftUI 底部工作表有时会忽略presentationDetents

保存和读取登录到 keys 串不工作 IOS swift

多协议和类继承混乱,Swift

如何在SwiftUI中实现淡入淡出加粘标题屏幕效果

如何在Combine发布者之间强制执行最小延迟

如何从单独的视图更改修饰符的值

在 iOS 上启用明文本地流量在 .NET MAUI 中不起作用

无法从 Xcode 获取模拟器列表.请打开 Xcode 并try 直接从那里运行项目以解决剩余问题

使用 Apollo 发布 iOS 应用程序时缺少推送通知权利

在视图控制器中实例化视图控制器是否可能/智能?

SwiftUI 中的偏移量和按钮有问题吗?

UIViewControllerRepresentable 在 Xcode 14 iOS 16 上崩溃

如何识别 SwiftUI 中点击的按钮 ID 以使该按钮动画化?

UIButton 事件.有什么不同?

Xcode 4.2 如何将一个项目包含到另一个项目中?

swift 语言中的 null / nil