SDKを8に変更してコンパイルすると, iphoneのlandscape時にステータスバーが自動で消えます, ipadはそのまま残ります XCode5のままだとステータスバーは残ります
landscape時に, より画面のスペースを確保するのが狙いらしいですが, アプリの作りによっては大問題です.
アプリの作り方における問題
UIを作る方法は,- storyboard
- xib
- コード
対策
ステータスバーの高さを動的に計算します. UIViewControllerのviewWillLayoutSubviews, UIViewのlayoutSubviewsメソッド内で, レイアウト(frame)の計算をおこないます. これらのメソッドは回転時にも呼ばれるので, そこで修正が可能です. ステータスバーの計算は以下で行いますCGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size; CGFloat statusBarHeight = statusBarSize.height
サンプル
headerとその下にborderを入れたものです. もちろんstoryboardは使用していません.
#import "ViewController.h"
#define kHEADERHEIGHT 44
@interface ViewController ()
@property (nonatomic) UIView *headerView;
@property (nonatomic) UIView *borderView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
// Add Header
self.headerView = [[UIView alloc] init];
[self.headerView setBackgroundColor:[UIColor cyanColor]];
self.borderView = [[UIView alloc] init];
[self.borderView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:self.headerView];
[self.view addSubview:self.borderView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillLayoutSubviews {
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
self.headerView.frame = CGRectMake(0, statusBarHeight, self.view.frame.size.width, kHEADERHEIGHT);
self.borderView.frame = CGRectMake(0, statusBarHeight + kHEADERHEIGHT, self.view.frame.size.width, 0.5);
}
@end
Swift
import UIKit
let kHEADERHEIGHT : CGFloat = 44.0
class ViewController: UIViewController {
var headerView : UIView
var borderView : UIView
override init() {
self.headerView = UIView()
self.borderView = UIView()
super.init()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
self.headerView = UIView()
self.borderView = UIView()
super.init(nibName: nibName, bundle: nibBundle)
}
required init(coder aDecoder: NSCoder) {
self.headerView = UIView()
self.borderView = UIView()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.headerView.backgroundColor = UIColor.cyanColor()
self.borderView.backgroundColor = UIColor.grayColor()
self.view.addSubview(self.headerView)
self.view.addSubview(self.borderView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillLayoutSubviews() {
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.height
self.headerView.frame = CGRectMake(0, statusBarHeight, self.view.frame.size.width, kHEADERHEIGHT)
self.borderView.frame = CGRectMake(0, statusBarHeight + kHEADERHEIGHT, self.view.frame.size.width, 0.5)
}
}
結果
iOS Simulator iOS8.0 iPhone4sでの結果です
portrait時です

landscape時です
ステータスバーがなくなりました.
これをiOS7.1のsimulatorでやると, ステータスバーは残ったままになります
0 件のコメント:
コメントを投稿