0

I'm trying to set a view that cover the entire screen. I set the frame = uiView.frame but there seems to be a space at the top and the frame center seems to be pushed down. Is there a way to force the view to fit the actual frame?

Code:

func showActivityIndicator(uiView: UIView) {
    container.frame = uiView.frame
    container.center = uiView.center
    container.backgroundColor = UIColor.blueColor() //UIColorFromHex(0xffffff, alpha: 0.3)

    loadingView.frame = CGRectMake(0, 0, 80, 80)
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2);

    loadingView.addSubview(activityIndicator)
    container.addSubview(loadingView)
    uiView.addSubview(container)
    activityIndicator.startAnimating()
}

Image (set bg color to blue to show what I mean)

enter image description here

Onichan
  • 4,476
  • 6
  • 31
  • 60

2 Answers2

1

Try setting the frame to (0,0,uiView.frame.size.width,uiView.frame.size.height) if you want to cover the view uiView. The outer view may not have it's origin at (0,0), but the inner view should have it's origin at (0,0) to cover the outer view.

container.frame = CGRectMake(0,0,uiView.frame.size.width,uiView.frame.size.height)
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • thank you. i ended up going with @p-double's solution as it seemed to get to the root of the issue. – Onichan Feb 19 '15 at 10:01
1

In order to cover the whole screen, you'll need to add the view to the main UIWindow, otherwise it will always sit under tab bars and navigation bars. There is also no need to set the centre and the frame. Just make sure the subviews frame is equal to the superviews bounds (not frame). See here for an explanation: UIView frame, bounds and center

Try this:

    // Get the main window
    let window: UIWindow = (UIApplication.sharedApplication().windows.first as UIWindow)

    // Match the size of the overlay view to the window
    container.frame = window.bounds

    // Add it to the window as a subview
    window.addSubview(overlayView)

If you wanted to add the loading indicator overlay under the navigation bar, just add it to the view controllers view. This code assumes it's being run inside the view controller (hence self = view controller)

    container.frame = self.view.bounds
    self.view.addSubview(container)
Community
  • 1
  • 1
Sam Clewlow
  • 4,293
  • 26
  • 36
  • Thank you. so when removing the subview window, can i still use `overlayView.removeFromSuperview()` – Onichan Feb 19 '15 at 10:00
  • yes, `UIWindow` is a subclass of `UIView`, so you can add and remove subviews in the same way you would with a normal view – Sam Clewlow Feb 19 '15 at 10:06
  • Thanks. I had a similar issue with another view that I used this for. But the navigation bar becomes unclickable (because the view is covering the entire window). is it possible to keep it under the navbar? – Onichan Feb 19 '15 at 10:11
  • 1
    No worries, yes I'll update my answer further for you – Sam Clewlow Feb 19 '15 at 11:26