使用导航栏注意事项获取屏幕中心 [英] Get the center of screen with Navigation Bar considerations

查看:24
本文介绍了使用导航栏注意事项获取屏幕中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 UIActivityIndi​​cator 居中并在我的应用程序中重复使用它.我面临的问题是我的某些视图必须位于导航栏下方,而有些则不是.这恰好使我的活动指示器在视图不在导航栏下方的视图中呈现低于中心的显示.例如:

I am trying to center a UIActivityIndicator and reuse it throughout my app. I'm facing an issue where some of my views have to be under the navigation bar, and some aren't. This happens to give my activity indicator a lower than center presentation on views where the view is not under the navigation bar. For instance:

StackOverflow 上的所有答案似乎都在图片 #2 之下.

All the answers on StackOverflow seem to fall under picture #2.

我只是对 UIView 进行了扩展,通过将其添加为子视图叠加来显示和隐藏活动指示器.检查 StackOverflow 上的多个帖子,我没有发现任何会重复这个问题的内容.

I've simply made an extension of UIView to show and hide the activity indicator by adding it as a subview overlay. Checking multiple posts on StackOverflow, I didn't find anything that would duplicate this question.

我的问题是,在生产应用程序中,如何仅使用一种功能成功地将这种视图集中在这两种情况下,但仍然支持 IOS 版本回到 9.3

My question is, in a production app, how would one successfully center this view for both of these situations utilizing only one function but still support IOS version back to 9.3

推荐答案

SWIFT 3:

Extension 将子视图居中放置在您的 App 委托的 keyWindow 中……无论它在您的应用程序中的任何位置,都使其居中:

Extension centers the subview in your App delegate's keyWindow...making it centered no matter where it is in your application:

extension UIView {

    public func addSubviewScreenCenter() {
        if let keyWindow = UIApplication.shared.keyWindow {
            keyWindow.addSubview(self)
            self.center()
        }
    }

    public func removeFromWindowView() {
        if self.superview != nil {
            self.removeFromSuperview()
        }
    }
}

这篇关于使用导航栏注意事项获取屏幕中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆