Программа c макетов показывает ошибку в ландшафтном режиме - PullRequest
0 голосов
/ 05 мая 2020

Вот скриншот, если ошибка Я создаю пользовательский интерфейс приложения программно, и когда я запускаю приложение, оно работает отлично, но когда я включаю моделирование в альбомном режиме, консоль показывает некоторые ошибки макетов. Можно ли просто указать ориентацию на портретную только в общих настройках приложения, потому что мое приложение отлично работает в портретном режиме, и я не хочу запускать приложение в любых других ориентациях, или у меня возникают проблемы с ограничениями? Вот мой код для ui ...

`let top = UIColor (красный: 217/255, зеленый: 30/255, синий: 133/255, alpha: 1) let bottom = UIColor (красный : 242/255, зеленый: 56/255, синий: 15/255, альфа: 1) let colorOne = UIColor (красный: 38/255, зеленый: 38/255, синий: 38/255, альфа: 1) let colorTwo = UIColor (красный: 0/255, зеленый: 0/255, синий: 0/255, альфа: 1) let colorTop = UIColor.red let 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)
    }

}
`

Ответы [ 2 ]

0 голосов
/ 05 мая 2020

Если вы хотите настроить ориентацию приложения, сделайте следующее: Go на Общие настройки проекта в Развертывание , измените ориентацию устройства.

0 голосов
/ 05 мая 2020

Вероятно, вы не делали автоматический макет.

Изменить: используйте это translatesAutoresizingMaskIntoConstraints = false после addSubView. Например,

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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...