【问题标题】:is there a way to automatically resize an image or any content based on the screen size in SWIFT?有没有办法根据 SWIFT 中的屏幕尺寸自动调整图像或任何内容的大小?
【发布时间】:2021-01-06 17:26:06
【问题描述】:

我一直在尝试找到一种方法来以编程方式根据屏幕尺寸调整图像大小而不使用情节提要?

【问题讨论】:

  • imageView.frame = view.bounds。当您将 imageView 添加到主视图时,您可以添加此代码。

标签: swift autolayout


【解决方案1】:

转到 google(或您最喜欢的搜索引擎)并搜索 swift add constraints programmatically。这是非常非常基本的。

这是一个简单的例子:

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // create image view
        let imgView = UIImageView()
        
        imgView.backgroundColor = .systemYellow
        
        // create image
        let img = UIImage(systemName: "person.fill")
        imgView.image = img
        
        // add imageView to view
        view.addSubview(imgView)
        
        // use auto-layout
        imgView.translatesAutoresizingMaskIntoConstraints = false
        
        // respect safe area
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([
            // constrain imageView width to view safe-area width
            imgView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            imgView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            // aquare image view (1:1 ratio)
            imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor),
            // center vertically
            imgView.centerYAnchor.constraint(equalTo: g.centerYAnchor),
        ])
        
    }
}

结果:


编辑 - 第二个例子...前三分之一,水平居中:

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.setNavigationBarHidden(true, animated: false)
        // create image view
        let imgView = UIImageView()
        
        imgView.backgroundColor = .systemYellow
        
        // create image
        let img = UIImage(systemName: "person.fill")
        imgView.image = img
        
        // add imageView to view
        view.addSubview(imgView)
        
        // use auto-layout
        imgView.translatesAutoresizingMaskIntoConstraints = false
        
        // respect safe area
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([
            
            // you want the image view at the top?
            imgView.topAnchor.constraint(equalTo: g.topAnchor),
            
            // one-third of the height of the view?
            imgView.heightAnchor.constraint(equalTo: g.heightAnchor, multiplier: 1.0 / 3.0),
            
            // you want a aquare image view (1:1 ratio)?
            imgView.widthAnchor.constraint(equalTo: imgView.heightAnchor),
            
            // you want it centered horizontally?
            imgView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            
        ])
        
    }
}

在 iPhone 8 上:

在 iPhone 12 上:

在 9.7 英寸 iPad Pro 上,横向:

【讨论】:

  • 如果你想根据屏幕大小来定位它呢?例如说我是我的标志在屏幕中间的上三分之一(没有故事板)。提前非常感谢你:)
  • @Mohamad3736 - 请参阅我的答案的编辑。我补充说作为另一个例子。但是,真的……这是基本的自动布局。请阅读文档并完成六个左右的教程。
  • 那太好了,谢谢你,但我的意思是,就像你在高度上加了一个乘数,说这张图片的高度应该是总视图的 1/3,有没有办法为 YAxis 值添加一个乘数,这样您就可以将图像放在中心上方,但不能放在最顶部(我尝试添加填充,但它会被较小尺寸的屏幕弄乱)。我问的原因是因为我想将两个对象放在另一个之上,而不仅仅是一个。我尝试查看文档,但他们的示例太简单了,没有太大帮助。
猜你喜欢
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
相关资源
最近更新 更多