点击状态栏时添加额外功能

时间:2015-08-18 12:00:17

标签: ios swift statusbar

我希望我的导航栏在点击状态栏时取消隐藏。目前,它只将我的网页视图滚动到顶部。谁知道如何使这个工作? webview仍然可以滚动到顶部,此功能不需要删除。

1 个答案:

答案 0 :(得分:1)

创建一个与状态栏尺寸相同的UIWindow,向UIButton添加UIWindow,并将其放置在状态栏的顶部。

import UIKit

class ViewController: UIViewController {

    let statusbarWindow = UIWindow()
    let statusbarButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Setup UIWindow
        statusbarWindow.frame = CGRectMake(0, 0,
            UIApplication.sharedApplication().statusBarFrame.size.width,
            UIApplication.sharedApplication().statusBarFrame.size.height)
        statusbarWindow.windowLevel = UIWindowLevelStatusBar
        statusbarWindow.makeKeyAndVisible()

        // Setup UIButton
        statusbarButton.frame = CGRectMake(0, 0,
            UIApplication.sharedApplication().statusBarFrame.size.width,
            UIApplication.sharedApplication().statusBarFrame.size.height)
        statusbarButton.backgroundColor = UIColor.clearColor()
        statusbarButton.addTarget(self, action: "statusbarButton:", forControlEvents: UIControlEvents.TouchUpInside)
        statusbarWindow.addSubview(statusbarButton)
    }

    func statusbarButton(sender:UIButton!) {
        // Scroll UIWebView to top
        yourWebView.scrollView.contentOffset = CGPoint(0,0)
        // Unhide nav bar here
    }

Apple文档https://developer.android.com/tools/publishing/app-signing.htmlUIWindowLevel