【问题标题】:Status bar changes color when searching搜索时状态栏变色
【发布时间】:2015-08-26 19:07:52
【问题描述】:

我正在使用UISearchController,单击它会使导航栏上升并将状态栏变为白色。我希望状态栏保持绿色。

我的UISearchController 实现:

self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.placeholder = "Pesquisa por linha ou cod..."
        controller.searchBar.tintColor = UIColor(netHex: 0xFFFFFF)
        controller.searchBar.barTintColor = UIColor(netHex: 0x2F7C30)
        controller.searchBar.clipsToBounds = true
        controller.searchBar.layer.borderWidth = 1;
        controller.searchBar.layer.borderColor = UIColor(netHex: 0x2F7C30).CGColor
        self.navigationController!.view.backgroundColor = UIColor(netHex: 0x2F7C30)

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

在点击搜索栏之前

点击搜索栏后

【问题讨论】:

  • 你最初是如何设置状态栏的颜色的?
  • 我正在我的 NavigationController 上设置导航栏背景颜色。

标签: ios iphone swift


【解决方案1】:

利用 Daniel Storm 的回应。将子视图添加到 UISearchController 的视图为我解决了这个问题。

let statusBarView = UIView(frame: CGRectMake(0, 0,
    UIApplication.sharedApplication().statusBarFrame.size.width,
    UIApplication.sharedApplication().statusBarFrame.size.height))
statusBarView.backgroundColor = Colors.Primary.Regular
searchController.view.addSubview(statusBarView)

【讨论】:

    【解决方案2】:

    在状态栏后面加上你需要的背景颜色UIView

    let statusBarView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
    statusBarView.backgroundColor = UIColor.greenColor() // Replace with color you desire
    view.addSubview(statusBarView)
    

    【讨论】:

    • 没用。该视图出现在导航栏下方和搜索栏前面。当点击搜索栏时,视图消失了?
    • @MarcioGranzotto 如果您在搜索栏激活时调用self.view.bringSubviewToFront(statusBarView) 会怎样?
    【解决方案3】:

    通过设置

    滚动边缘外观

    按钮外观

    后退按钮外观

    对我来说很好,这里是代码 sn-p

    func setNavigationBarAppreance(){
    
                let navBar = UINavigationBar.appearance()
        
                let navigationBarAppearance = UINavigationBarAppearance()
       
                navigationBarAppearance.configureWithOpaqueBackground()
                navigationBarAppearance.backgroundColor = .primary // replace your desired color
                navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.navigationBarTintColor]
                navigationBarAppearance.shadowImage = UIImage()
                navigationBarAppearance.backgroundImage = UIImage()
    
                navBar.scrollEdgeAppearance = navigationBarAppearance
                navBar.compactAppearance = navigationBarAppearance
                navBar.standardAppearance = navigationBarAppearance
                navBar.isTranslucent = false
                navBar.tintColor = .navigationBarTintColor
                navBar.barTintColor = .navBarColor
    }
    

    只需在所需的 Viewcontrollers 中使用此功能,也可以将其放入 AppDelegate 文件中进行全局外观设置

    【讨论】:

      【解决方案4】:

      navigationImage 设置 backgroundImage 就足够了

      navigationController?.navigationBar.setBackgroundImage(UIImage(from: UIColor(hexString: kMainColorHex, alpha: 1)), for: .any, barMetrics: .default)
      

      【讨论】:

        【解决方案5】:

        试试这个:

        self.navigationController?.navigationBar.isTranslucent = false
        

        【讨论】:

          【解决方案6】:

          对于 Objective C 版本,您可以使用以下代码:

          UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].statusBarFrame.size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
          statusBarView.backgroundColor = [UIColor searchBarBackgroundColor];
          

          [resultSearchController.view addSubview:statusBarView];

          【讨论】:

            猜你喜欢
            • 2016-03-17
            • 1970-01-01
            • 1970-01-01
            • 2013-09-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多