如何在iOS swift中将subView添加到具有居中对齐的anotherSubView。

时间:2018-03-28 04:27:20

标签: ios swift xcode

我正在使用lottie库进行动画制作。我将lottieFile作为subView加载到anotherSubView但它没有在中心对齐。我尝试使用如下的中心属性:

public function store(Request $request)
{
    $path = $request->file('something')->store('upload');
    echo $path;
}

Result not aligned

4 个答案:

答案 0 :(得分:5)

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

在这里,我添加了两种方法来添加animationView中心对齐,还添加了注释以便理解。

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))
        }

答案 1 :(得分:3)

enter image description here

您可以通过编程方式执行以下代码,我希望这可以帮助您

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

    }

}

答案 2 :(得分:1)

@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 = CGRectMake(viewOn.frame.origin.x, viewOn.frame.origin.y, viewOn.frame.size.width, viewOn.frame.size.height)
viewOn.addSubview(animationView)
}

你可以尝试一下,希望它没关系

答案 3 :(得分:0)

我的Lottie动画的对齐和缩放存在几个问题。它们将集中显示在模拟器上,而不显示在设备上。经过几个小时的调试,我发现布局问题正在发生,因为我正在viewDidLoad()上设置动画。当我将代码移至viewDidAppear(_ animated: Bool)时,一切都像灵符一样工作。我希望这可以帮助其他人。