Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

ios - Align label's center.x with image inside tabBar's imageView

I need to get a label's center.x directly aligned with the image inside a tabBar's imageView. Using the below code the label is misaligned, instead of the label's text "123" being directly over the bell inside the tabBar, it's off to the right.

enter image description here

guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }

guard let fourthTab = tabBarController?.tabBar.items?[3].value(forKey: "view") as? UIView else { return }

guard let imageView = fourthTab.subviews.compactMap({ $0 as? UIImageView }).first else { return }

guard let imageViewRectInWindow = imageView.superview?.superview?.convert(fourthTab.frame, to: keyWindow) else { return }

let imageRect = AVMakeRect(aspectRatio: imageView.image!.size, insideRect: imageViewRectInWindow)

myLabel.text = "123"
myLabel.textAlignment = .center // I also tried .left
myLabel.center.x = imageRect.midX
myLabel.center.y = UIScreen.main.bounds.height - 74
myLabel.frame.size.width = 50
myLabel.frame.size.height = 21

print("imageViewRectInWindow: (imageViewRectInWindow)") // (249.99999999403948, 688.0, 79.00000000298022, 48.0)
print("imageRect: (imageRect)") // (265.4999999955296, 688.0, 48.0, 48.0)
print("myLabelRect: (myLabel.frame)") // (289.4999999955296, 662.0, 50.0, 21.0)
question from:https://stackoverflow.com/questions/65845332/align-labels-center-x-with-image-inside-tabbars-imageview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It might be a layout issue, as in, setting the coordinates before everything is laid out. Where do you call the code from? I was able to get it to work with the following, but got strange results with some if the functions you use, so cut out a couple of them. Using the frame of the tabview worked for me, and calling the coordinate setting from the view controller's viewDidLayoutSubviews function.

class ViewController: UIViewController {

    var myLabel: UILabel = UILabel()

    var secondTab: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.addSubview(myLabel)
        myLabel.textColor = .black
        myLabel.text = "123"
        myLabel.textAlignment = .center // I also tried .left
        myLabel.frame.size.width = 50
        myLabel.frame.size.height = 21

        secondTab = tabBarController?.tabBar.items?[1].value(forKey: "view") as? UIView
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        guard let secondTab = secondTab else {
            return
        }

        myLabel.center.x = secondTab.frame.midX
        myLabel.center.y = UIScreen.main.bounds.height - 70
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...