代码之家  ›  专栏  ›  技术社区  ›  Rue Vitale

定位UILabel

  •  0
  • Rue Vitale  · 技术社区  · 11 月前

    我使用以下代码在屏幕上定位UILabel

       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)
        
    

    不幸的是,UILabel被渲染在靠近电池和wifi的最顶部。我甚至看不见它。但我想知道它是否能在不同的iPhone上正常渲染。标签的正确定位方式是什么?我不想使用自动布局。

    见附图。

    enter image description here

    2 回复  |  直到 11 月前
        1
  •  1
  •   GoksuBayy    11 月前

    使用NSLayoutConstraint代替CGRect(x,y,width,height)。

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

    希望有帮助

    编辑:如果这不起作用。获取safeArea并使用其约束

    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)
    )]
    
        2
  •  1
  •   Deepak Bhardwaj    11 月前

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