1

I have found this code to add GIF Background on the landing screen and worked great,

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

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

    let webViewBG = UIWebView(frame: self.view.frame)
    webViewBG.loadData(gif!, MIMEType: "image/gif", textEncodingName: "utf-8", baseURL: NSURL())

    webViewBG.userInteractionEnabled = false;
    self.view.addSubview(webViewBG)

    let filter = UIView()
    filter.frame = self.view.frame
    filter.backgroundColor = UIColor.blackColor()
    filter.alpha = 0.05
    self.view.addSubview(filter)

    let welcomeLabel = UILabel(frame: CGRectMake(0, 100, self.view.bounds.size.width, 100))
    welcomeLabel.text = "KeepON"
    welcomeLabel.textColor = UIColor.whiteColor()
    welcomeLabel.font = UIFont(name: "Zapfino", size: 35)
    welcomeLabel.textAlignment = NSTextAlignment.Center
    self.view.addSubview(welcomeLabel)
}

this shows my GIF background and my Title, but When I try to add Buttons in story board in View Controller they don't appear when I run my app, only the GIF background with the title, and I don't want to add buttons using code.

So any ideas ? Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

This is because you are adding subviews on top of the views that already exist. Instead of addSubView, use insertSubView(webViewBG,atIndex:0);

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • thanks , any idea about how to resize the GiF ? because it appear smaller with auto layout ON , thanks – Hussein Reda AlBehary Oct 13 '15 at 08:22
  • This is a web view. So i have no idea, is there any particular reason you want it as a web view? Because uiimageview supports animation, and it is a lot better than having a web browser for a view. Btw if this answered your question please mark it as an answer so people know. – Knight0fDragon Oct 13 '15 at 11:36
  • no there is no particular reason that was the only working solution I found, so I have to just choose my GIF as I choose a normal image ? and thanks for the info I didn't know that xD – Hussein Reda AlBehary Oct 13 '15 at 17:12
  • Take a look at this, it will help you out. If you go the ImageView route, it has scaling abilities for the image. http://stackoverflow.com/questions/4386675/add-animated-gif-image-in-iphone-uiimageview – Knight0fDragon Oct 13 '15 at 17:22
  • hey sorry for the late reply, but the code you provided in link is in objective-c not swift 2 :/ !! – Hussein Reda AlBehary Oct 22 '15 at 05:16
  • Can't hold your hand the entire way, converting from objective C to swift 2 is really easy, plus if you look at the other answers, sombody has posted a swift 2 answer – Knight0fDragon Oct 22 '15 at 14:25
  • 1
    yes, but you could look at the objective C, look at the swift, and learn how to get the answer from the 2 – Knight0fDragon Oct 22 '15 at 17:18