【问题标题】:iOS 7 status bar like iOS 6iOS 7 状态栏类似于 iOS 6
【发布时间】:2013-09-20 12:56:18
【问题描述】:

我有一个支持横向和纵向模式的应用。而且我需要像在 iOS 6 上一样的行为状态栏。最简单的方法是什么?

我已经尝试过 Stack Overflow 问题 iOS 7 status bar back to iOS 6 style? 中的解决方案,但它不起作用。我的子视图取决于视图大小,并且我的视图无法正确拉伸。我不想更新我的 XIB 文件;我只是想添加一些对我有帮助的东西。我不知道它可能是什么(黑客或祈祷)。

【问题讨论】:

  • 是否必须针对 iOS 7 SDK 进行编译?如果您不需要 iOS 7 中的任何内容,那么您应该能够简单地针对 SDK 6 进行编译。至少目前是这样。使用 SDK6 编译并带有适当的操作系统目标的应用程序应该仍然具有 7 之前的 iOS 的所有外观和感觉。
  • 我有类似的问题。我将 XCode 降级到 4.6.3 版,现在一切正常(在任何装有 iOS 5-7 的设备上)。更改 SDK 并没有解决问题,所以 XCode 5.0 有一些烦人的问题。
  • 只是不明白为什么这个问题被关闭了。显然,状态栏样式是 iOS 编程中的重要知识,这个问题可以帮助很多 iOS 程序员。采用 iOS7 不仅仅是改变一个 png。
  • 您的解决方案完美运行,谢谢 :)
  • 天哪,不要那样做。混搭你的代码的其他部分将使用的方法,加上 Apple 的框架是灾难的秘诀......

标签: ios objective-c ios7


【解决方案1】:

您可以尝试在 ViewWillappear 或 DidAppear 中编写此内容。在这里,我们将视图框架向下移动了 20 个像素。

CGRect frame = self.view.frame;
frame.origin.y = 20;

if (self.view.frame.size.height == 1024 || 
    self.view.frame.size.height == 768)
{
    frame.size.height -= 20;
}

self.view.frame = frame;

这会起作用,但这不是一个好主意。如果有帮助,您还可以通过调用以下方法将状态栏的文本颜色更改为浅色或深色,具体取决于您的应用背景。

-(UIStatusBarStyle)preferredStatusBarStyle
{
     return UIStatusBarStyleLightContent; // For light status bar

     return UIStatusBarStyleDefault // For Dark status bar
}

【讨论】:

  • 我只想把一些代码放在一个地方,而不是每个地方都在应用程序中。 UIViewController 上的 Swizzling 方法不好的解决方案:因为我可以 [viewController1.view addSubview:viewController2.view]
  • 尝试将上面的代码放在 viewController1 的 DidAppear 方法中。因为如果你想让你的应用看起来像 iOS 6 ,几乎没有其他方法。另一种选择是将所有子视图向下移动 20 像素,但工作量很大
  • 谢谢,如果我没有找到任何简单的解决方案,我会使用你的解决方案。我不想要这个,因为我有很多应用程序有很多视图。
【解决方案2】:

如果您使用的是 Xcode 5 并且正在安装 iOS 7,那么抱歉,这不会发生(据我所知)。

如果您想在 iOS 7 上查看状态栏,而不是在 Xcode 4.x.x 中打开项目并在 iOS 7 中安装。我发现这种方法的一个问题是有时 Xcode 4.x.x 无法识别iOS 7 设备。

但如果您的 Xcode 4.x.x 可以显示您的 iOS 7 设备,那么它就可以工作。

从 Xcode 4.x.x 生成的 .api 将在 iOS 6 和 iOS 7 中工作,但你不会在 iOS 7 上获得额外的空间(状态栏)以及键盘、选择器、开关等的新外观. 但是是的,你会得到新的 UIAlertView (我不知道为什么这是新的而其他控件是旧的。)

我希望我们很快会在 Xcode 5 中为此获得更好的解决方案。

更新:

我找到了从 Xcode 5 作为 Xcode 4 运行应用程序的方法。这只是基本 SDK 的问题。 如果您想从 Xcode 5 构建为 Xcode 4 (iOS 6 SDK),请执行以下操作。

  1. 关闭 Xcode 4 和 5。

  2. 在 Xcode 4 中转到

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs

  3. 在这里您可以找到iPhoneOS6.1.sdk。复制此文件夹。现在在同一条路径上进入 Xcode 5。在 Xcode 5 中,您将找到 iPhoneOS7.0.sdk。粘贴iPhoneOS6.1.sdk

  4. 现在关闭 Finder 并启动 Xcode 5。转到 project target setting -> Build Setting 并找到 Base SDK。选择 iOS 6.1 作为基础 SDK。这也适用于 6.0。你只需要找到iPhoneOS6.0.sdk

  5. 现在您将在运行下拉框中看到两次设备名称。一种用于 SDK 7.0,另一种用于 SDK 6.1。所以现在您可以使用 iOS 6 SDK 和 iOS 7 SDK 两种方式运行。

我希望这会对某人有所帮助。

【讨论】:

  • 我想将我的应用上传到应用商店。据我所知,我没有机会使用 xCode 4.6.x
  • 我在网上尝试了很多解决方案,但对我没有任何帮助。您可以从 Xcode 4.6.x 上传我已经从 xcode 4.6.x 上传了两个应用程序并且工作正常。你还需要 iOS 7 大小的图标。
  • 我很想知道。
  • 我找到了这个解决方案stackoverflow.com/questions/18294872/…
  • 这是不正确的。您可以在 Xcode 5 中查看 iOS 6。从 Interface Builder 的 Storyboard Settings 中选择它。永远不要使用旧版本的 Xcode。
【解决方案3】:

我最近不得不解决一个类似的问题,但我的处理方式略有不同......

方法是使用一个额外的视图控制器,作为我最初的 rootViewController 的容器视图控制器。首先,我设置了一个这样的容器:

_containerView = [[UIView alloc] initWithFrame:[self containerFrame]];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_containerView.clipsToBounds = YES;

[self.view addSubview:_containerView];
[self.view setBackgroundColor:[UIColor blackColor]];

[UIApplication.sharedApplication setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];

containerFrame 的定义如下:

- (CGRect)containerFrame
{
    if ([MyUtilityClass isSevenOrHigher])
    {
        CGFloat statusBarHeight = [MyUtility statusBarHeight]; //20.0f
        return CGRectMake(0, statusBarHeight, self.view.bounds.size.width, self.view.bounds.size.height - statusBarHeight);
    }

    return self.view.bounds;
}

最后,我将原来的 rootViewController 添加为新的 childViewController:

//Add the ChildViewController
self.childController.view.frame = self.containerView.bounds;
self.childController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self addChildViewController:self.childController];
[self.containerView addSubview:self.childController.view];
[self.childController didMoveToParentViewController:self];

注意事项: - 模态视图控制器仍将以 iOS7 样式呈现,所以我仍然必须以某种方式考虑这一点。

希望这对某人有所帮助!

【讨论】:

    【解决方案4】:

    本指南对我有帮助。

    http://www.doubleencore.com/2013/09/developers-guide-to-the-ios-7-status-bar/

    处理 20 磅大小差异的最可靠方法是自动布局。

    如果您不使用自动布局,Interface Builder 为您提供了处理 iOS 7 和旧版本之间屏幕尺寸差异的工具。当 Auto Layout 关闭时,您会注意到 Interface Builder 实用程序区域(右窗格)的大小选项卡中的一个区域,允许您设置 iOS 6/7 Deltas。

    【讨论】:

      【解决方案5】:

      1) 这是一个 hack,但它有效!

      如果你不使用 UIAlertView 或 KGStatusBar 就使用它!!!

      #import <objc/runtime.h>
      
      @interface UIScreen (I_love_ios_7)
      - (CGRect)bounds2;
      - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation;
      @end
      
      @implementation UIScreen (I_love_ios_7)
      - (CGRect)bounds2
      {
          return [self boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
      }
      
      - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation
      {
          CGRect resultFrame = [self bounds2];
          if(UIInterfaceOrientationIsLandscape(orientation))
              resultFrame.size.width -= 20;
          else
              resultFrame.size.height -= 20;
          return resultFrame;
      }
      @end
      
      void Swizzle(Class c, SEL orig, SEL new)
      {
          Method origMethod = class_getInstanceMethod(c, orig);
          Method newMethod = class_getInstanceMethod(c, new);
          if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
              class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
          else
              method_exchangeImplementations(origMethod, newMethod);
      }
      
      
      @implementation AppDelegate
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      
          if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
              Swizzle([UIScreen class], @selector(bounds2), @selector(bounds));
              [application setStatusBarStyle:UIStatusBarStyleLightContent];
              self.window.clipsToBounds =YES;
      
              [[NSNotificationCenter defaultCenter] addObserver:self
                                                       selector:@selector(applicationDidChangeStatusBarOrientation:)
                                                           name:UIApplicationWillChangeStatusBarOrientationNotification
                                                         object:nil];
              NSDictionary* userInfo = @{UIApplicationStatusBarOrientationUserInfoKey : @([[UIApplication sharedApplication] statusBarOrientation])};
              [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillChangeStatusBarOrientationNotification
                                                                  object:nil
                                                                userInfo:userInfo];
          }
      
          return YES;
      }
      
      - (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
      {
          UIInterfaceOrientation orientation = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
          CGSize size = [[UIScreen mainScreen] boundsForOrientation:orientation].size;
          int w = size.width;
          int h = size.height;
          float statusHeight = 20.0;
          switch(orientation){
              case UIInterfaceOrientationPortrait:
                  self.window.frame =  CGRectMake(0,statusHeight,w,h);
                  break;
              case UIInterfaceOrientationPortraitUpsideDown:
                  self.window.frame =  CGRectMake(0,0,w,h);
                  break;
              case UIInterfaceOrientationLandscapeLeft:
                  self.window.frame =  CGRectMake(statusHeight,0,w,h);
                  break;
              case UIInterfaceOrientationLandscapeRight:
                  self.window.frame =  CGRectMake(0,0,w,h);
                  break;
          }
      }
      @end
      

      2) 创建类别,并始终使用contentView 而不是view

      @interface UIViewController(iOS7_Fix)
      @property (nonatomic, readonly) UIView* contentView;
      - (void)updateViewIfIOS_7;
      @end
      
      @implementation UIViewController(iOS7_Fix)
      static char defaultHashKey;
      - (UIView *)contentView
      {
          return objc_getAssociatedObject(self, &defaultHashKey)?: self.view;
      }
      
      - (void)setContentView:(UIView *)val
      {
          objc_setAssociatedObject(self, &defaultHashKey, val, OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;
      }
      
      - (void)updateViewIfIOS_7
      {
          if([[[UIDevice currentDevice] systemVersion] floatValue] < 7 || objc_getAssociatedObject(self, &defaultHashKey))
              return;
      
          UIView* exchangeView = [[UIView alloc] initWithFrame:self.view.bounds];
          exchangeView.autoresizingMask = self.view.autoresizingMask;
          exchangeView.backgroundColor = [UIColor blackColor];
      
          UIView* view = self.view;
          if(self.view.superview){
              [view.superview addSubview:exchangeView];
              [view removeFromSuperview];
          }
          [exchangeView addSubview:view];
          self.view = exchangeView;
      
          CGRect frame = self.view.bounds;
          frame.origin.y += 20.0;
          frame.size.height -= 20.0;
          view.frame = frame;
          view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
      
          [self setContentView:view];
      }
      

      在每个UIViewController:

      - (void)viewDidLoad
      {
          [super viewDidLoad];
          [self updateViewIfIOS_7];
          UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
          lab.backgroundColor = [UIColor yellowColor];
          [self.contentView addSubview:lab];
          //...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        • 2013-08-14
        • 2015-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多