-1

I was wondering how can i show a 'No Internet Connection' Just how like Instagram does it,

As an Example :

enter image description here

That see-through custom message animating to show under the navigationController . Would really love to get this to my project , thank you for you help

Community
  • 1
  • 1
RandomGeek
  • 303
  • 5
  • 20

1 Answers1

8

So here's a pic of the storyboard like this :-

"No internet connection" is a label, and the red view underneath is just to test the see through property of the label. If you are designing the UI in code, you can probably make a label similar to mine and place it to the top of the Navigation bar by using it's frame property.

Storyboard scene

The button here I'm using is just to show the label pop up on the scene (since it's just a demo answer). In your case, if the internet is not available, you will proceed to show the pop up.

So if you are making the UI in code, make sure to make the label in the viewDidLoad method. I have made an IBOutlet and the viewDidLoad now looks like this:-

override func viewDidLoad() {
        super.viewDidLoad()

        let transform = CGAffineTransform(translationX: 0, y: -label.frame.height)
        label.alpha = 0
        label.transform = transform
    }

On the view loading, I'm moving the label behind the navigation bar, using CGAffineTransform. The distance, how much to move up is the label's height, since we don't want any part to be clipped on the scene.

Next step, is just a fix. I'm making alpha = 0, because navBar is translucent is nature and hence will change it's colour, since our label is behind it. So setting alpha to 0, takes care of it, and in third step apply the transform.

Now, if the internet connection is not available, we should pop out the label under the navBar. The code will look something like this:-

fun checkInternet() {
// called by some of your observer, which checks for changes in internet connection

    if !isInternetAvailable {

       UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveLinear, animations: {

        self.label.alpha = 0.5
        self.label.transform = .identity
    }, completion: nil)
     }
}

So here, I'll show the pop up with an animation using UIView.animate with some spring damping, so it has a nice bouncy effect to it. I'm setting the alpha to 0.5, since you mentioned you want a see through label, and I'm setting the label to a transform which will bring it back to it's original position when it was created, that's why I'm using .identity. You can play around usingSpringWithDamping values and change options to have different effects.

Dark Innocence
  • 1,389
  • 9
  • 17