Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 在横向模式下显示错误的编程布局_Ios_Swift - Fatal编程技术网

Ios 在横向模式下显示错误的编程布局

Ios 在横向模式下显示错误的编程布局,ios,swift,Ios,Swift,我正在以编程方式构建应用程序的ui,当我运行应用程序时,它运行得非常完美,但当我以横向模式打开模拟时,控制台显示一些布局错误。因为我的应用程序在纵向模式下运行得非常完美,我不想在任何其他方向上运行应用程序,或者我在约束方面遇到了一些问题,所以只在常规应用程序设置中将方向勾选为纵向可以吗?这是我的ui代码 `让top=ui颜色(红色:217/255,绿色:30/255,蓝色:133/255,alpha:1) 让底部=UIColor(红色:242/255,绿色:56/255,蓝色:15/255,al

我正在以编程方式构建应用程序的ui,当我运行应用程序时,它运行得非常完美,但当我以横向模式打开模拟时,控制台显示一些布局错误。因为我的应用程序在纵向模式下运行得非常完美,我不想在任何其他方向上运行应用程序,或者我在约束方面遇到了一些问题,所以只在常规应用程序设置中将方向勾选为纵向可以吗?这是我的ui代码

`让top=ui颜色(红色:217/255,绿色:30/255,蓝色:133/255,alpha:1) 让底部=UIColor(红色:242/255,绿色:56/255,蓝色:15/255,alpha:1) 设colorOne=UIColor(红色:38/255,绿色:38/255,蓝色:38/255,alpha:1) 设colorTwo=UIColor(红色:0/255,绿色:0/255,蓝色:0/255,alpha:1) 让colorTop=UIColor.red 让colorBottom=UIColor.green

    let topContainer: UIView = {
        let top = UIView()
        top.translatesAutoresizingMaskIntoConstraints = false
        top.backgroundColor = .black
        return top
    }()
    let model: UIImageView = {
        let mymodel = UIImageView(image: #imageLiteral(resourceName: "bodybuilder"))
        mymodel.translatesAutoresizingMaskIntoConstraints = false
        mymodel.contentMode = .scaleToFill
        return mymodel
    }()
    let logo: UIImageView = {
        let gymble = UIImageView(image: #imageLiteral(resourceName: "Gymble"))
        gymble.contentMode = .scaleAspectFill
        gymble.translatesAutoresizingMaskIntoConstraints = false
        return gymble
    }()

    let bottomContainer: UIView = {
        let bottom = UIView()
        bottom.translatesAutoresizingMaskIntoConstraints = false
        return bottom
    }()

    let pinField: UITextField = {
        let phone = UITextField()
        phone.backgroundColor = .white
        phone.text = "+91"
        phone.textAlignment = .center
        phone.layer.cornerRadius = 5
        phone.font = UIFont(name: "Roboto-Regular", size: 20)
        phone.keyboardType = UIKeyboardType.numberPad
        phone.translatesAutoresizingMaskIntoConstraints = false
        return phone
    }()


    let phoneFeild: UITextField = {
        let phone = UITextField()
        phone.backgroundColor = .white
        phone.placeholder = "Phone number"
        phone.layer.cornerRadius = 5
        phone.font = UIFont(name: "Roboto-Regular", size: 20)
        phone.keyboardType = UIKeyboardType.phonePad
        phone.keyboardAppearance = UIKeyboardAppearance.dark
        phone.translatesAutoresizingMaskIntoConstraints = false
        return phone
    }()

    let loginButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("Login", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.layer.cornerRadius = 5
        button.layer.masksToBounds = true
        button.titleLabel?.font = UIFont(name: "Roboto-Medium", size: 22)
        button.addTarget(self, action: #selector(someButtonAction), for: .touchUpInside)
        return button
    }()

    let laterButton: UIButton = {
        let button = UIButton(type: .system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("I'll login later", for: .normal)
        button.setTitleColor(.gray, for: .normal)
        button.titleLabel?.font = UIFont(name: "Roboto-Light", size: 16)
        return button
    }()

    let horizontalStack: UIStackView = {
        let mystack = UIStackView()
        mystack.alignment = UIStackView.Alignment.center
        mystack.axis = NSLayoutConstraint.Axis.horizontal
        mystack.translatesAutoresizingMaskIntoConstraints = false
        mystack.distribution = .fill
        mystack.spacing = 15
        return mystack
    }()

    let stack: UIStackView = {
        let mystack = UIStackView()
        mystack.alignment = UIStackView.Alignment.center
        mystack.axis = NSLayoutConstraint.Axis.vertical
        mystack.translatesAutoresizingMaskIntoConstraints = false
        mystack.distribution = .equalSpacing
        return mystack
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black

        view.addSubview(topContainer)
        topContainerLayout()

        topContainer.addSubview(model)
        modelLayout()

        view.addSubview(bottomContainer)
        bottomContainerLayout()

        bottomContainer.addSubview(stack)
        stackLayout()

        stack.addArrangedSubview(logo)
        logoLayout()

        stack.addArrangedSubview(horizontalStack)
        horizontalStackLayout()
        horizontalStack.addArrangedSubview(pinField)
        pinFieldLayout()

        let padding = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: self.phoneFeild.frame.height))
        phoneFeild.leftView = padding
        phoneFeild.leftViewMode = UITextField.ViewMode.always
        horizontalStack.addArrangedSubview(phoneFeild)
        phoneFieldLayout()


        let gradientWidth = (UIScreen.main.bounds.width - 32)
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [top.cgColor, bottom.cgColor]
        gradientLayer.locations = [0.15, 1]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        gradientLayer.frame = CGRect(x: 0, y: 0, width: gradientWidth, height: 50)
        gradientLayer.cornerRadius = 5
        loginButton.layer.insertSublayer(gradientLayer, at: 0)

        stack.addArrangedSubview(loginButton)
        loginButtonLayout()

        stack.addArrangedSubview(laterButton)
        LaterButtonLayout()

    }

    func LaterButtonLayout(){
        laterButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    }

    func loginButtonLayout(){
        loginButton.widthAnchor.constraint(equalTo: stack.widthAnchor, constant: -32).isActive = true
        loginButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
    }

    func logoLayout(){
        logo.topAnchor.constraint(equalTo: stack.topAnchor).isActive = true
        logo.heightAnchor.constraint(equalToConstant: 56).isActive = true
        logo.widthAnchor.constraint(equalToConstant: 60).isActive = true
    }

    func stackLayout(){
        stack.heightAnchor.constraint(equalTo: bottomContainer.heightAnchor).isActive = true
        stack.widthAnchor.constraint(equalTo: bottomContainer.widthAnchor).isActive = true
    }

    func horizontalStackLayout(){
        horizontalStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
        horizontalStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
        horizontalStack.heightAnchor.constraint(equalToConstant: 40).isActive = true
    }
    func pinFieldLayout(){
        pinField.leadingAnchor.constraint(equalTo: horizontalStack.leadingAnchor).isActive = true
        pinField.topAnchor.constraint(equalTo: horizontalStack.topAnchor).isActive = true
        pinField.bottomAnchor.constraint(equalTo: horizontalStack.bottomAnchor).isActive = true
        pinField.widthAnchor.constraint(equalToConstant: 50).isActive = true
    }
    func phoneFieldLayout(){
        phoneFeild.leadingAnchor.constraint(equalTo: pinField.trailingAnchor, constant: 15).isActive = true
        phoneFeild.topAnchor.constraint(equalTo: horizontalStack.topAnchor).isActive = true
        phoneFeild.bottomAnchor.constraint(equalTo: horizontalStack.bottomAnchor).isActive = true
    }

    func modelLayout(){
        model.topAnchor.constraint(equalTo: topContainer.topAnchor).isActive = true
        model.leadingAnchor.constraint(equalTo: topContainer.leadingAnchor).isActive = true
        model.trailingAnchor.constraint(equalTo: topContainer.trailingAnchor).isActive = true
        model.heightAnchor.constraint(equalTo: topContainer.heightAnchor).isActive = true
    }


    func bottomContainerLayout(){
        bottomContainer.topAnchor.constraint(equalTo: topContainer.bottomAnchor).isActive = true
        bottomContainer.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
        bottomContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        bottomContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }

    func topContainerLayout(){
        topContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        topContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 2/3 , constant: -20).isActive = true
        topContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        topContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }
    @objc func someButtonAction(){
        print("Button is tapped!")
        let opt = OTPViewController()
        present(opt, animated: true, completion: nil)
    }

}
`

可能你没有做自动布局

  • 请查看此->
  • 而这个->
编辑: 在addSubView之后使用translatesAutoResizengMaskintoConstraints=false

func topContainerLayout(){
    topContainer.translatesAutoresizingMaskIntoConstraints = false
    topContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    topContainer.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 2/3 , constant: -20).isActive = true
    topContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    topContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}

如果要自定义应用程序方向,请执行以下操作: 转到项目的常规设置
在部署中,更改设备方向。

添加与UI相关的错误和代码。我在顶部容器的闭包中添加了此属性。我知道,我刚才说从属性中删除它并添加到topContainerLayout函数。为所有人做这件事。