【问题标题】:Navigation bar and Status bar issue in iOS 7iOS 7 中的导航栏和状态栏问题
【发布时间】:2014-06-30 06:48:18
【问题描述】:

我已经使用XIB 创建了一个应用程序,之前没有Status Bar 用于iOS7,现在我需要在我的应用程序上添加Status bar 并且Status bar background color 应该与Navigation bar 背景颜色相同。所以我尝试过(在我的info.plist):

1) Set View controller-based status bar appearance to NO
2) Set Status bar style to UIStatusBarStyleLightContent 

这是我的App Delegate 代码:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1.0]];

所以我得到如下图所示的输出:

我在屏幕下方给出的 UIButtons 也出现错位(它隐藏了 20 个像素)。

你能帮我解决这个问题吗?我需要如下图所示的输出:

任何帮助将不胜感激,谢谢。

第三屏:

【问题讨论】:

  • 您正在剪辑到边界,并且边界的 origin.y 为 20px。这似乎是问题所在?
  • @Danqing谢谢,你能告诉我我需要做什么改变吗?
  • 为什么窗口的y坐标是20?
  • 好吧,我猜先把这两行去掉?
  • 感谢@NagaMalleshMaddali,现在我已经删除了窗口 y 坐标(20 像素)..所以我得到了像我附加的第一张图片一样的输出,我已经更新了我的问题请检查

标签: iphone ios7 navigationbar ios7-statusbar


【解决方案1】:

在 viewDidLoad 方法中添加以下代码:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

if (systemVersion >= 7.0) 
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

【讨论】:

  • Mallesh Maddali 我在 viewDidLoad 方法中添加了相同的内容,但仍然得到与图 1 相同的输出。如何分隔状态栏和导航栏?
  • 尝试从 AppDelegate 中删除以下代码: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  • 谢谢@Naga 我已经删除了提到的行。现在我得到没有状态栏的输出。
  • 只添加这一行来检查:[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
  • 添加后..我得到的输出是状态栏作为我的附加图像 3.请检查..
【解决方案2】:

请在 AppDelegate.h(或)constants.h 中定义它

  #define isIOS7  ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0)?YES:NO

在 viewDidLoad 下写下这几行。

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
   UIView *addStatusBar = [[UIView alloc] init];
   addStatusBar.frame = CGRectMake(0, 0, 320, 20);
    addStatusBar.backgroundColor = [UIColor colorWithRed:0.973 green:0.973 blue:0.973 alpha:1];


     //change this to match your navigation bar
    [self.window.rootViewController.view addSubview:addStatusBar];
  }

或者我们可以在 xib 中手动更改它。

将每个元素向下移动 20 像素。

【讨论】:

    【解决方案3】:

    对很少有人提出这个建议感到惊讶。这是正确的方法(禁止黑客攻击)

    首先,让您的导航栏的 Y 原点为 20。

    然后在 .h 文件中,将您的 ViewController 设置为 UIBarPositioningDelegate:

    @interface XYZViewController : UIViewController <UIBarPositioningDelegate>
    

    在.m文件中:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationBar.delegate = self;
    }
    
    - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { 
        return UIBarPositionTopAttached; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多