在 Swift 代码中,我的标签没有出现在我的 ViewController 中
In Swift code my Label is not showing up in my ViewController
当ViewController显示在phone上时Label不显示
我是 iOS 和 swift 的新手,我在 YouTube 上进行了广泛的搜索,但无济于事。在代码中,您可以看到我已两次尝试添加两个标签。并不是说 none 正在显示。导航元素工作正常。背景也设置正确。
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
}
}
y 值应大于 64 即。 20 个状态栏 + 44 个导航栏。
此外,您不需要 drawText 和 frame tile。
只需添加
pageTitle.frame = CGRect(x: 20, y: 200, width: UIScreen.main.bounds.width - 20, height: 50)
会起作用。
当ViewController显示在phone上时Label不显示
我是 iOS 和 swift 的新手,我在 YouTube 上进行了广泛的搜索,但无济于事。在代码中,您可以看到我已两次尝试添加两个标签。并不是说 none 正在显示。导航元素工作正常。背景也设置正确。
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
}
}
y 值应大于 64 即。 20 个状态栏 + 44 个导航栏。 此外,您不需要 drawText 和 frame tile。 只需添加
pageTitle.frame = CGRect(x: 20, y: 200, width: UIScreen.main.bounds.width - 20, height: 50)
会起作用。