skip to Main Content

I’m using the following code to position the UILabel on the screen

   innerView = UIView(frame: view.bounds)
    self.view.addSubview(innerView)
    
    let headingLbl = UILabel(frame: CGRect(x: 20, y: 20, width: innerView.frame.width - 40, height: 30))
    headingLbl.font = UIFont(name: "Inter-Bold", size: 20);
    headingLbl.text = "What are your goals?"
    headingLbl.textAlignment = .center
    headingLbl.textColor = .black
    innerView.addSubview(headingLbl)
    

Unfortunately, the UILabel gets rendered at the very top near the battery and wifi. I can’t even see it. But I’m wondering if it will render properly on different iPhones. What’s the right way to position the label? I don’t want to use AutoLayout.

See attached image.

enter image description here

2

Answers


  1. Instead using CGRect(x, y, width, height) use NSLayoutConstraint.

     innerView = UIView(frame: view.bounds)
    self.view.addSubview(innerView)
    
    let headingLbl = UILabel()
    headingLbl.font = UIFont(name: "Inter-Bold", size: 20);
    headingLbl.text = "What are your goals?"
    headingLbl.textAlignment = .center
    headingLbl.textColor = .black
    headingLbl.translatesAutoresizingMaskIntoConstraints = false // Use it to set constraint correctly
    innerView.addSubview(headingLbl)
    
    
    NSLayoutConstraint.activate([
      headingLbl.topAnchor.constraint(equalTo: innerView.topAnchor, constant: 20),
      headingLbl.leadingAnchor.constraint(equalTo: innerView.leadingAnchor, constant: 20),
      headingLbl.trailingAnchor.constraint(equalTo: innerView.trailingAnchor, constant: -20),
      headingLbl.heightAnchor.constraint(equalToConstant: 30)
    )]
    

    Hope it helps

    Edit: If that does not work. Get safeArea and use their’s constraint

    let safeArea = innerView.safeAreaLayoutGuide
    
    NSLayoutConstraint.activate([
      headingLbl.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 20),
      headingLbl.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 20),
      headingLbl.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -20),
      headingLbl.heightAnchor.constraint(equalToConstant: 30)
    )]
    
    Login or Signup to reply.
  2. Use safeAreaInsets

    enter code here
    

    var innerView = UIView()

    enter code here
    override func viewDidLoad() {
        super.viewDidLoad()
        let safeAreaInsets = view.safeAreaInsets
    
        innerView = UIView(frame: CGRect(x: safeAreaInsets.left,
                                         y: safeAreaInsets.top,
                                         width: view.frame.width - safeAreaInsets.left - safeAreaInsets.right,
                                         height: view.frame.height - safeAreaInsets.top - safeAreaInsets.bottom))
        self.view.addSubview(innerView)
        let headingLbl = UILabel(frame: CGRect(x:  20, y: 20, width: innerView.frame.width - 40, height: 30))
        headingLbl.font = UIFont(name: "Inter-Bold", size: 20);
        headingLbl.text = "What are your goals?"
        headingLbl.textAlignment = .center
        headingLbl.textColor = .black
        innerView.addSubview(headingLbl)
        innerView.backgroundColor = .red
        self.view.addSubview(innerView)
    
    }
    override func viewDidLayoutSubviews() {
         super.viewDidLayoutSubviews()
    
         // Adjust the frame after layout to account for any changes in safe area insets
         if let customView = view.subviews.first {
             let safeAreaInsets = view.safeAreaInsets
             customView.frame = CGRect(x: safeAreaInsets.left,
                                       y: safeAreaInsets.top,
                                       width: view.frame.width - safeAreaInsets.left - safeAreaInsets.right,
                                       height: view.frame.height - safeAreaInsets.top - safeAreaInsets.bottom)
         }
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search