隐藏UINavigationBar而不会丢失状态栏背景

时间:2016-08-25 22:19:10

标签: ios objective-c swift uinavigationcontroller uinavigationbar

我正在开发一个应用程序,我们使用白色状态栏色调和navigationBar的深色背景。有一个场景我们希望导航栏隐藏,但它也会消除状态栏的背景颜色。是否有一个简单的解决方案可以保持暗背景同时隐藏navigationBar?

隐藏导航栏的代码是:

[self.navigationController setNavigationBarHidden:YES];

或在Swift中:

self.navigationController?.navigationBarHidden = true

3 个答案:

答案 0 :(得分:0)

假设您正在为iOS7 +开发:状态栏没有'有自己的背景颜色。实际上,当导航栏可见时,您看到深色背景的原因是因为它在状态栏下方向上延伸。因此,如果要保留状态栏背景,只需将具有适当背景颜色的视图添加到当前场景(viewController,window等)。给它一个package com.zetcode; import java.awt.event.ActionEvent; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; import net.miginfocom.swing.MigLayout; /* * Centering components on screen. * Author Jan Bodnar * Website zetcode.com */ class MyDialog extends JDialog { public MyDialog() { initDialog(); } private void initDialog() { JLabel lbl1 = new JLabel("Code"); JTextField field1 = new JTextField(15); JLabel lbl2 = new JLabel("Password"); JPasswordField field2 = new JPasswordField(15); JButton btn = new JButton("Login"); createLayout(lbl1, field1, lbl2, field2, btn); setTitle("Login"); setLocationRelativeTo(null); setDefaultCloseOperation(HIDE_ON_CLOSE); setModal(true); } private void createLayout(JComponent... arg) { setLayout(new MigLayout("wrap, align 50% 50%", "[center]")); add(arg[0]); add(arg[1]); add(arg[2]); add(arg[3]); add(arg[4]); pack(); } } public class MigLayoutCenterEx extends JFrame { public MigLayoutCenterEx() { initUI(); } private void initUI() { JPanel pnl = new JPanel(); JButton btn = new JButton("Show dialog"); btn.addActionListener((ActionEvent e) -> { JDialog dialog = new MyDialog(); dialog.setVisible(true); }); pnl.add(btn); add(pnl); setSize(400, 300); setTitle("MigLayout example"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { MigLayoutCenterEx ex = new MigLayoutCenterEx(); ex.setVisible(true); }); } } 的框架。

- 更新1 -

示例代码(Swift 3)为您提供稳定的黑色状态栏背景:

UIApplication.sharedApplication.statusBarFrame


- 更新2 -

虽然我们正在努力。以上代码不是您应该如何布置您的观点。相反,子类class ViewController: UIViewController { private let statusBarUnderlay = UIView() override func viewDidLoad() { super.viewDidLoad() statusBarUnderlay.backgroundColor = UIColor.black view.addSubview(statusBarUnderlay) } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() statusBarUnderlay.frame = UIApplication.shared.statusBarFrame } } 并在那里进行布局。然后覆盖UIView子类的loadView并返回自定义视图的实例。


UIViewController

答案 1 :(得分:0)

以下是LTNavigationBar

的实施

我认为它可以帮到你。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > 0) {
        if (offsetY >= 44) {
            [self setNavigationBarTransformProgress:1];
        } else {
            [self setNavigationBarTransformProgress:(offsetY / 44)];
        }
    } else {
        [self setNavigationBarTransformProgress:0];
        self.navigationController.navigationBar.backIndicatorImage = [UIImage new];
    }
}

- (void)setNavigationBarTransformProgress:(CGFloat)progress
{
    [self.navigationController.navigationBar.transform = CGAffineTransformMakeTranslation:(0, -44 * progress)];
}

当滚动视图时,隐藏导航栏和状态栏与导航栏具有相同的背景颜色。如果您不需要滚动,则只需拨打[self setNavigationBarTransformProgress:1]

答案 2 :(得分:0)

我需要做的就是添加一个辅助视图并将其设置为状态栏下方,如下所示:

-(void)viewForStatusBar {
    UIView *view = [UIView new];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor blackColor];        
    view.frame = CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 20);
}

或者在Swift中:

func viewForStatusBar() {
        let view = UIView()
        self.view.addSubview(view)
        view.backgroundColor = .blackColor()
        view.frame = CGRectMake(0, -20, UIScreen.mainScreen().bounds.size.width, 20)
    }