Onichan
Onichan

Reputation: 4526

Frame of SubView is Shifted Down

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

Upvotes: 0

Views: 445

Answers (2)

Sam Clewlow
Sam Clewlow

Reputation: 4351

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)

Upvotes: 1

rakeshbs
rakeshbs

Reputation: 24572

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)

Upvotes: 1

Related Questions