0

When the ViewController is displayed on the phone the Label is not displayed

I am new to iOS and swift, I have search YouTube extensively to no avail. In the code you can see I have made two attempts to add two labels. Not that none are showing. The navigation elements are working correctly. The background is being set correctly also.

import UIKit

class WelcomeController: UIViewController {

    // MARK: - Properties

    var pageTitle: UILabel!

    // MARK: - Init

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        let frameLabel:CGRect = CGRect(x: 20, y: 0, width: UIScreen.main.bounds.width - 20, height: 50)
        let label:UILabel = UILabel(frame: frameLabel)
        label.text = "This text is not showing up on the screen!!! his text is not showing up on the screen!!! his text is not showing up on the screen!!!"
        label.textColor = .black
        label.textAlignment = .center
        view.addSubview(label)

        configureUI()

        configurePageTitleLabel()

    }

    // MARK: - Selectors

    @objc func handleDismiss() {
        dismiss(animated: true, completion: nil)
    }

    // MARK: - Helper Functions

    func configureUI() {

        navigationController?.navigationBar.barTintColor = .blue
        navigationItem.title = "Welcome"
        let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        navigationController?.navigationBar.titleTextAttributes = textAttributes

        navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_menu_white_3x").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))

    }

    // MARK - Page Contents Functions

    func configurePageTitleLabel() {

        pageTitle = UILabel()
        pageTitle.text = "Welcome"
        pageTitle.textAlignment = .center
        pageTitle.textColor = .black
        let frameTitle:CGRect = CGRect(x: 20, y: 20, width: UIScreen.main.bounds.width - 20, height: 50)
        pageTitle.drawText(in: frameTitle)
        view.addSubview(pageTitle)
        view.backgroundColor = .gray

    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
MyerJ
  • 3
  • 3

1 Answers1

0

The y value should be more than 64 ie. 20 status bar + 44 Navigation bar. Also, you don't need drawText and frame tile. Simply adding

pageTitle.frame = CGRect(x: 20, y: 200, width: UIScreen.main.bounds.width - 20, height: 50)

would work.

Bhagyesh
  • 144
  • 1
  • 8