简体   繁体   中英

Swift 3 ios: UILabel needs to appear on top of a UIImage when the Image appears

So I am trying to give the user the ability to design their own virtual business card where the user can drag a label around the UIView, they can change background color of the UIView, as well as the color of the UILabel. The card itself is represented by a nib and the user selects the different color choices or template image choices through a UIPickerView. I want the Label to appear on top of the Image when it appears and is dragged to that part of the View, however it is not currently doing that. Changing colors of the background for the UIView and the text work just fine; I am also able to add and remove the Template image (which is a PNG) by using .isHidden().

My Problem is that when the UIImage is added to the UIView it is on top of the UILabel. If part of the UILabel overlaps with part of the png image it disappears behind it, and I would like it to appear over the image.

在图像被取消隐藏之前,UIView的外观如何

取消隐藏test_triangle.png时标签在图像下的显示方式

The code for the nib is in userCardView.swift and the picker view is on the mainViewController where I set the UserDefaults which works fine.

class userCardView: UIView {

   @IBOutlet var cardView: UIView!
   @IBOutlet weak var testLBL: UILabel!
   @IBOutlet weak var templateImage: UIImageView!

   var colorIndex = ["Black", "White", "Gray"]
   var templateIndex = ["None", "Triangle"]

   override func awakeFromNib() {
       super.awakeFromNib()

       cardView.clipToBounds = true 

       let backColorObject = UserDefaults.standard.object(forKey: "backGroundColor") as? String
       let textColorObject = UserDefaults.standard.object(forKey: "textColor") as? String
       let templateColorObject = UserDefaults.standard.object(forKey: "templateColor") as? String
       let templateImageObject = UserDefaults.standard.object(forKey: "templateImage") as? String

       for _ in templateIndex {
           if templateImageObject == templateIndex[1] {
               templateImage.isHidden = false
               templateImage.image = UIImage(named: "test_triangle.png")
               //testLBL.bringSubview(toFront: templateImage)
               templateImage.bringSubview(toFront: testLBL)
           } else if templateImageObject == templateIndex[0] {
               templateImage.isHidden = true
           } else {
               templateImage.isHidden = true
           }
       }

I'm Still pretty new to using swift so any help at all would be greatly appreciated, thank you.

You need to ask testLBL 's superview to bring it to the front. Your code templateImage.bringSubview(toFront: testLBL) won't work if templateImage is not the parent view of testLBL because testLBL is not in templateImage 's view hierarchy.

So try to use the superview of testLBL

<testLBL's parent view>.bringSubview(toFront: testLBL)

or

testLBL.parentView.bringSubview(toFront: testLBL)

Hope it solves your problem

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