简体   繁体   中英

How can I add text in the center of a UIView Programmatically?

I have the following UIView, how can text be added in the center of this UIView programmatically?

This is the UIView code:

    let newView = UIView()
    newView.backgroundColor = .groupTableViewBackground
    self.view.addSubview(newView)
    newView.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 11.0, *) {
        let guide = self.view.safeAreaLayoutGuide
        newView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        newView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        newView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
        newView.heightAnchor.constraint(equalToConstant: 50).isActive = true
    } else {
        NSLayoutConstraint(item: newView,
                           attribute: .top,
                           relatedBy: .equal,
                           toItem: view, attribute: .top,
                           multiplier: 1.0, constant: 0).isActive = true
        NSLayoutConstraint(item: newView,
                           attribute: .leading,
                           relatedBy: .equal, toItem: view,
                           attribute: .leading,
                           multiplier: 1.0,
                           constant: 0).isActive = true
        NSLayoutConstraint(item: newView, attribute: .trailing,
                           relatedBy: .equal,
                           toItem: view,
                           attribute: .trailing,
                           multiplier: 1.0,
                           constant: 0).isActive = true

        newView.heightAnchor.constraint(equalToConstant: 50).isActive = true
    }

This was my attempt at adding text to the center of the UIView programmatically, but I'm not sure why it is not working?

let lb = UILabel()
lb.centerXAnchor.constraint(equalTo: newView.centerXAnchor).isActive = true
lb.centerYAnchor.constraint(equalTo: newView.centerYAnchor).isActive = true
lb.text="anything"
newView.backgroundColor = UIColor.white

// show on screen
self.view.addSubview(newView)
newView.addSubview(lb)
lb.center = newView.center

UPDATE:

How can this button border below the UIView that's under the navigation controller be added?

在此输入图像描述

When it comes to adding constraints to the view programmatically, note that you have to set translatesAutoresizingMaskIntoConstraints to false (but I get the feeling that you already know this, and just forgot to add it to the label)

let lb = UILabel()
lb.textAlignment = .center
lb.numberOfLines = 0

newView.addSubview(lb)

lb.translatesAutoresizingMaskIntoConstraints = false
lb.centerXAnchor.constraint(equalTo: newView.centerXAnchor).isActive = true
lb.centerYAnchor.constraint(equalTo: newView.centerYAnchor).isActive = true

Add border (assuming you want to add the border to the newView ):

let border = UIView()
newView.addSubview(border)

border.translatesAutoresizingMaskIntoConstraints = false

border.leadingAnchor.constraint(equalTo: newView.leadingAnchor).isActive = true
border.trailingAnchor.constraint(equalTo: newView.trailingAnchor).isActive = true
border.bottomAnchor.constraint(equalTo: newView.bottomAnchor).isActive = true
border.heightAnchor.constraint(equalToConstant: 1).isActive = true

But for the border it would probably be cleaner to write an extension on UIView, so the code can be reusable in other places, if you ever need it again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM