使用自动布局将对象均匀分开

Posted

技术标签:

【中文标题】使用自动布局将对象均匀分开【英文标题】:use auto layout to space objects evenly apart 【发布时间】:2019-12-11 05:51:18 【问题描述】:

我下面的代码试图基于下面的照片。 PicLocate 应该占据视图高度的 60%。 Pic[0] 和 Pic1 应该占据 10% 的高度并且应该具有均匀的宽度。所有的对象应该在视图之间间隔 10 - 15 个间隙。主要是应该和照片一模一样

    import UIKit
class ViewController: UIViewController 
var picLocate = UIImageView()
var jessicaAlba:Float = 50
var topConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!
var leadingConstraint: NSLayoutConstraint!
var trailingConstraint: NSLayoutConstraint!

let pic = (0..<3).map  _ in UIButton() 


override func viewDidLoad() 
    super.viewDidLoad()

    [picLocate,pic[0],pic[1]].forEach 
        $0.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview($0)
        $0.backgroundColor = .systemOrange
    


    pic[0].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10).isActive = true
    pic[0].leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
    pic[0].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -300).isActive = true
    pic[0].bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -300).isActive = true

    pic[1].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10).isActive = true
    pic[1].leadingAnchor.constraint(equalTo: pic[0].trailingAnchor, constant: 10).isActive = true
    pic[1].trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true
    pic[1].bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -300).isActive = true



    pic[0].setTitle("Add Box", for: .normal)
    pic[1].setTitle("Save Photo", for: .normal)




    topConstraint = picLocate.topAnchor.constraint(equalTo: view.topAnchor, constant: CGFloat(jessicaAlba))
    topConstraint.isActive = true
    heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: CGFloat(-jessicaAlba))
    heightConstraint.isActive = true

    leadingConstraint = picLocate.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: CGFloat(jessicaAlba))
    leadingConstraint.isActive = true
    trailingConstraint = picLocate.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: CGFloat(-jessicaAlba))
    trailingConstraint.isActive = true





【问题讨论】:

尝试使用乘数来限制您的高度 @AlexandrKolesnik 你能告诉我一些代码吗?谢谢。 【参考方案1】:

在编写约束时注释它们会很有帮助。很多时候,这会让你意识到你想做什么......

要获得所需的布局,您需要:

picLocate 高度设置为视图高度的 60%(使用您当前的代码,您还从该 60% 中减去 50 分) 将左侧图片(按钮)的前导设置为picLocate 的前导 将右侧图片(按钮)的尾部设置为picLocate 的尾部 将右图的前导设置为左图的尾随 + 10 分作为间距 然后设置右图宽度等于左图宽度

结果:

并且,在横向时,它会自动调整到所需的百分比:

这是您的代码,其中包含我描述的更改。看一看,复习一下 cmets:

class ViewController: UIViewController 

    var picLocate = UIImageView()

    var jessicaAlba:CGFloat = 50

    var topConstraint: NSLayoutConstraint!
    var heightConstraint: NSLayoutConstraint!
    var leadingConstraint: NSLayoutConstraint!
    var trailingConstraint: NSLayoutConstraint!

    let pic = (0..<3).map  _ in UIButton() 

    override func viewDidLoad() 
        super.viewDidLoad()

        view.backgroundColor = .white

        [picLocate,pic[0],pic[1]].forEach 
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.backgroundColor = .systemOrange
        

        // 50-pts (jessicaAlba) from top of view
        topConstraint = picLocate.topAnchor.constraint(equalTo: view.topAnchor, constant: jessicaAlba)

        // 60% of view height minus 50-pts == ((0.6 * view height) - 50)
        heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: -jessicaAlba)

        // if you actually want 60% of view height, set constant to 0.0
        //heightConstraint = picLocate.heightAnchor.constraint(equalTo: view.heightAnchor , multiplier: 0.6, constant: 0.0)

        // 50-pts leading and trailing
        leadingConstraint = picLocate.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: jessicaAlba)
        trailingConstraint = picLocate.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -jessicaAlba)

        NSLayoutConstraint.activate([

            topConstraint,
            heightConstraint,
            leadingConstraint,
            trailingConstraint,

            // "left pic" top is 10-pts below "picLocate" bottom
            pic[0].topAnchor.constraint(equalTo: picLocate.bottomAnchor, constant: 10.0),

            // "left pic" leading equal to "picLocate" leading
            pic[0].leadingAnchor.constraint(equalTo: picLocate.leadingAnchor, constant: 0.0),

            // "left pic" height equal to 10% of view height
            pic[0].heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.10),

            // "right pic" top equal to "left pic" top
            pic[1].topAnchor.constraint(equalTo: pic[0].topAnchor),

            // "right pic" height equal to "left pic" height
            pic[1].heightAnchor.constraint(equalTo: pic[0].heightAnchor),

            // "right pic" trailing equal to "picLocate" trailing
            pic[1].trailingAnchor.constraint(equalTo: picLocate.trailingAnchor, constant: 0.0),

            // "right pic" leading equal to "left pic" trailing plust 10-pts
            pic[1].leadingAnchor.constraint(equalTo: pic[0].trailingAnchor, constant: 10.0),

            // "right pic" width equal to "left pic" width
            pic[1].widthAnchor.constraint(equalTo: pic[0].widthAnchor),

        ])

        pic[0].setTitle("Add Box", for: .normal)
        pic[1].setTitle("Save Photo", for: .normal)

    

【讨论】:

以上是关于使用自动布局将对象均匀分开的主要内容,如果未能解决你的问题,请参考以下文章

使用自动布局在屏幕上水平均匀分布按钮。它们之间的间距不相等

使用自动布局 VFL 垂直堆叠一组按钮

Swift UIView 小部件自动布局不起作用

如何使用自动布局在对象之间创建间隙

删除特定对象的自动布局约束

在 iOS 6 中使用 Auto Layout 在 superview 中均匀分布相同大小的 UIView