使用Swift在代码中创建的中心UILabel [英] Center UILabel created in code using Swift

查看:68
本文介绍了使用Swift在代码中创建的中心UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是您在Swift中的Xcode中可能遇到的最简单的事情,由于某种原因,它无法正常工作.

我想在视图中居中放置标签.以前视图中唯一的另一件事是以编程方式添加了webView,但现在我已将其删除,因此,基本上,我有一个空的VC,试图在其中居中放置标签.

解决方案

在添加约束之前,将 loadingLabel 添加为子视图.

  view.addSubview(loadingLabel)loadingLabel.translatesAutoresizingMaskIntoConstraints = falseloadingLabel.centerXAnchor.constraint(equalTo:view.centerXAnchor).isActive = trueloadingLabel.centerYAnchor.constraint(equalTo:view.centerYAnchor).isActive = true 

This may be the simplest thing you can possibly due in Xcode in Swift and for some reason, it is not working properly.

I want to center a label in a view. The only other thing in the view previously was a webView added programatically but for now I have removed that so basically, I have an empty VC in which I'm trying to center a label.

There are umpteen answers on SO about this and I've tried every combination but can't get it to to work.

Can anyone suggest a foolproof way to accomplish the simple task of centering a UILabel?

Below is the code I currently have and steps I've taken along with result:

I created an empty view controller in Storyboard and embedded it in a navigation controller. I set the View Controller in Storyboard to my swift VC class. I also have already cleaned project, closed and re-opened XCode and also deleted storyboard and recreated it in case it was corrupted. Still nothing works.

 myVC.swift
 import UIKit

class myVC: UIViewController,WKScriptMessageHandler, WKNavigationDelegate,WKUIDelegate {
    
    var title= "Hello there"     
    var loadingLabel = UILabel()
 
   override func viewDidLoad() {
          super.viewDidLoad()
        webView.navigationDelegate = self
        webView.uiDelegate = self
         loadingLabel.translatesAutoresizingMaskIntoConstraints = false  
      // loadingLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
       // loadingLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
       // loadingLabel = UILabel(frame: CGRect(x: 0, y: self.view.center.y, width: 290, height: 70))
        loadingLabel.center = self.view.center
        loadingLabel.textAlignment = .center
       loadingLabel.font = UIFont(name: "Halvetica", size: 18.0)
        loadingLabel.numberOfLines = 0
        loadingLabel.text = "TEXT I WANT TO CENTER"
        loadingLabel.lineBreakMode = .byTruncatingTail
        loadingLabel.center = self.view.center
        self.view.addSubview(loadingLabel)     
        self.title = title
    }        
    override func loadView() {
        super.loadView()
     }
}

解决方案

Add the loadingLabel as subview before adding the constraints.

view.addSubview(loadingLabel)
loadingLabel.translatesAutoresizingMaskIntoConstraints = false 
loadingLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

这篇关于使用Swift在代码中创建的中心UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆