SubView框架向下移动

时间:2015-02-19 09:28:19

标签: ios objective-c xcode swift

我试图设置一个覆盖整个屏幕的视图。我设置了frame = uiView.frame,但顶部似乎有一个空格,frame center似乎被推倒了。有没有办法强制视图适合实际的框架?

代码:

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()
}

图像(将bg颜色设置为蓝色以显示我的意思)

enter image description here

2 个答案:

答案 0 :(得分:1)

如果要覆盖视图(0,0,uiView.frame.size.width,uiView.frame.size.height),请尝试将框架设置为uiView。外部视图可能没有origin (0,0),但内部视图应该origin (0,0)覆盖外部视图

container.frame = CGRectMake(0,0,uiView.frame.size.width,uiView.frame.size.height)

答案 1 :(得分:1)

为了覆盖整个屏幕,您需要将视图添加到主UIWindow,否则它将始终位于标签栏和导航栏下方。也没有必要设置中心和框架。只需确保子视图框架等于超视图边界(不是框架)。请参阅此处获取解释:UIView frame, bounds and center

试试这个:

    // 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)

如果要在导航栏下添加加载指示符叠加层,只需将其添加到视图控制器视图即可。此代码假定它在视图控制器内运行(因此自身=视图控制器)

    container.frame = self.view.bounds
    self.view.addSubview(container)