Basil Ahmad Basil Ahmad - 1 year ago 68
Swift Question

Add tools with GIF background (Swift)

I already typed codes for some tools(text/button/label), but once i put a gif background they disappeared. the question is .. How to get them into the foreground?

override func viewDidLoad() {

let filePath = NSBundle.mainBundle().pathForResource("tiger", ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)

let webViewBG = UIWebView(frame: self.view.frame)
webViewBG.loadData(gif!, MIMEType: "image/gif", textEncodingName: String(), baseURL: NSURL())
webViewBG.userInteractionEnabled = false;

Answer Source

Looking at your code I'd say that when you call addSubview method, the view you are adding is on the foreground.

You have to call one of these methods to manipulate view hierarchy :

view.sendSubviewToBack(view: UIView)
view.bringSubviewToFront(view: UIView)

What I'd do is to add the subview, then send it to the background with sendSubviewToBack method

UPDATE : Seems that it doesn't work that way. There will be several solutions you could use. Either you keep your web view and your view separated but with the same frame so that your view is just on top of your web view.

Problem is that if your frame changes, you have to change the frames of both views.

Another solution is to add your label inside your web view (didn't test it though but it works with an image view so I assume it may work).