Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS swift中将子视图添加到具有中心对齐的另一个子视图。_Ios_Swift_Xcode - Fatal编程技术网

如何在iOS swift中将子视图添加到具有中心对齐的另一个子视图。

如何在iOS swift中将子视图添加到具有中心对齐的另一个子视图。,ios,swift,xcode,Ios,Swift,Xcode,我正在用洛蒂图书馆制作动画。我将lottieFile作为子视图加载到另一个子视图,但它没有在中心对齐。我尝试使用中心属性,如下所示: @IBOutlet weak var viewOn: UIView! let animationView = LOTAnimationView(name: "restless_gift_ii") { animationView.loopAnimation = true animationView.contentMode = .scaleToFill animat

我正在用洛蒂图书馆制作动画。我将lottieFile作为子视图加载到另一个子视图,但它没有在中心对齐。我尝试使用中心属性,如下所示:

@IBOutlet weak var viewOn: UIView!

let animationView = LOTAnimationView(name: "restless_gift_ii") {
animationView.loopAnimation = true
animationView.contentMode = .scaleToFill
animationView.animationSpeed = 1
animationView.backgroundColor = UIColor.black
animationView.frame.size = viewOn.frame.size
animationView.center.x = viewOn.center.x
animationView.center.y = viewOn.center.y
viewOn.addSubview(animationView) }

您可以像下面的代码一样以编程的方式完成它,我希望这对您有所帮助

import UIKit

class ViewController: UIViewController {

    let firstView:UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = UIColor.blue
        return view
    }()

    let secondView:UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = UIColor.orange
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupViews()
    }

    func setupViews(){

        view.addSubview(firstView)
        firstView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive = true
        firstView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
        firstView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        firstView.heightAnchor.constraint(equalToConstant: 100).isActive = true

        // second view
        firstView.addSubview(secondView)
        secondView.centerYAnchor.constraint(equalTo: firstView.centerYAnchor, constant: 0).isActive = true
        secondView.centerXAnchor.constraint(equalTo: firstView.centerXAnchor, constant: 0).isActive = true
        secondView.widthAnchor.constraint(equalToConstant: 50).isActive = true
        secondView.heightAnchor.constraint(equalToConstant: 50).isActive = true

    }

}

可以通过编程方式使用“自动布局”将动画视图居中对齐到其超级视图中

在这里,我添加了两种方法来添加
animationView
center align,还添加了注释以便于理解

if let animationView = LOTAnimationView(name: "4_bar_loop") {

        animationView.loopAnimation = true
        animationView.contentMode = .scaleToFill
        animationView.animationSpeed = 1
        animationView.backgroundColor = UIColor.black

        self.viewOn.addSubview(animationView)

        animationView.translatesAutoresizingMaskIntoConstraints = false

        // Apply these constrains if you want animation size should be same as super view.
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .leading, relatedBy: .equal, toItem: self.viewOn, attribute: .leading, multiplier: 1.0, constant: 1))
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .trailing, relatedBy: .equal, toItem: self.viewOn, attribute: .trailing, multiplier: 1.0, constant: 1))
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .top, relatedBy: .equal, toItem: self.viewOn, attribute:.top, multiplier: 1.0, constant: 1))
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .bottom, relatedBy: .equal, toItem: self.viewOn, attribute: .bottom, multiplier: 1.0, constant: 1))


        // Apply these constraint if you want animationView with fixed height and width and center of super view.
//            self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .centerX, relatedBy: .equal, toItem: self.viewOn, attribute: .centerX, multiplier: 1.0, constant: 1))
//            self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .centerY, relatedBy: .equal, toItem: self.viewOn, attribute: .centerY, multiplier: 1.0, constant: 1))
//            animationView.addConstraint(NSLayoutConstraint(item: animationView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//            animationView.addConstraint(NSLayoutConstraint(item: animationView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        }

你可以试试,希望没问题

我的乐蒂动画在对齐和缩放方面遇到了一些问题。它们将集中显示在emulator上,而不是设备上。调试了几个小时后,我发现出现了布局问题,因为我正在
viewDidLoad()
上设置动画。当我将代码移动到
viewdide(uu动画:Bool)
时,一切都像一个魔咒一样工作。我希望这可以帮助其他人

@IBOutlet weak var viewOn: UIView!

let animationView = LOTAnimationView(name: "restless_gift_ii")
{
    animationView.loopAnimation = true
    animationView.contentMode = .scaleToFill
    animationView.animationSpeed = 1
    animationView.backgroundColor = UIColor.black
    animationView.frame = viewOn.frame
    viewOn.addSubview(animationView)
}