iOS状态栏隐藏

在做葡萄相机的时候,使用 项目 info.plist  文件配置的方法,隐藏了所有的页面导航栏。但是在做滤镜选项卡页面的时候,自定义了一个 视图。里面有一个 UIWindow 对象。重新指定了根视图,于是,这个页面状态栏又出现了。发现无法用代码控制隐藏。

#define MOVE_DISTANCE 7

@interface EditViewControllerCustomScrollView()
{
    //关闭、确定 按钮
    UIButton *cancelButton;
    UIButton *confirmButton;
    
    UIScrollView *filterScrollView; //当前页面显示滚动视图,在当前页面切换滤镜效果
    NSMutableArray *filterImageArr;  //保存9 种滤镜效果数组
    NSMutableArray *filterTitleArr;  //保存9 种滤镜名称数组
    
    NSInteger selectedFilter;
}

@property (nonatomic, assign) CGRect         screenRect;      //当前屏幕尺寸,兼容横竖屏幕
@property (nonatomic, strong) UIWindow       *window;         //弹出 Sheet 所在Window
@property (nonatomic, strong) UIView         *dimBackground;  //弹出后,原来的视图覆盖的背景

@end


@implementation EditViewControllerCustomScrollView


#pragma mark -  初始化
//根据滚动视图需要的数据进行初始化
- (instancetype)initWithFilterImageArray:(NSMutableArray *)filterArray withFilterTitleArray:(NSMutableArray *)nameArray withShowHiddenDelegate:(id<ItemShowHiddenDelegate>)delegateHidden withSelectedDelegate:(id<ItemSelectedDelegate>)delegateSelected andSelectedID:(NSInteger)selectedID
{
    self = [super init];
    if (self) {
        //
        //根据横竖屏幕,获取当前屏幕尺寸
        //
        _screenRect = [UIScreen mainScreen].bounds;
        if ([[UIDevice currentDevice].systemVersion floatValue] < 7.5 &&
            UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
            _screenRect = CGRectMake(0, 0, _screenRect.size.height, _screenRect.size.width);
        }
        
        filterImageArr = filterArray;
        filterTitleArr = nameArray;
        delegateVC = delegateHidden;
        delegateSelectVC = delegateSelected;
        selectedFilter = selectedID;
        
        
        //
        //创建新的视图,用于点击空白隐藏 sheetView
        //
        _dimBackground = [[UIView alloc] initWithFrame:_screenRect];
        _dimBackground.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
        [_dimBackground addGestureRecognizer:gr];
        
        //
        //sheetView 本身的背景色
        //
        self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];
        
        /*calculate action sheet frame begin*/
        CGFloat height = 0.0;
        
        //cancel button screenwidth*60
        height = EIDTVC_BUTTONMENUBAR_HEIGHT+95;
        /*calculation end*/
        self.frame = CGRectMake(0, _screenRect.size.height, _screenRect.size.width, height);
    }
    return self;
}


#pragma mark - 显示隐藏自定义视图
//always show in a new window
- (void)show
{
    //根据当前屏幕尺寸,创建一个新的 Window
    self.window = [[UIWindow alloc] initWithFrame:self.screenRect];
    self.window.windowLevel = UIWindowLevelAlert;
    self.window.backgroundColor = [UIColor clearColor];
    
    //获取根视图控制器
    self.window.rootViewController = [UIViewController new];
    self.window.rootViewController.view.backgroundColor = [UIColor clearColor];
    
    //
    //把发挥作用的两个视图都添加到控制器里面
    //
    [self.window.rootViewController.view addSubview:self.dimBackground];//确保点击空白隐藏 SheetView
    
    [self.window.rootViewController.view addSubview:self]; //确保 SheetView 可见
    
    self.window.hidden = NO;
    
    [self createFilterScrollView];
    [self createFooterView];
    
    [UIView animateWithDuration:0.2 animations:^{
        //隐藏导航栏
        [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
        
        self.dimBackground.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2];
        
        self.frame = CGRectMake(0, self.screenRect.size.height-self.frame.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
    }];

}

- (void)hidden
{
    [UIView animateWithDuration:0.2 animations:^{
        self.dimBackground.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(0, self.screenRect.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        self.window = nil;
    }];
    
    //
    //调用代理方法
    //
    if(delegateVC!=nil) {
        [delegateVC itemHidden];
    }
}

 

解决办法:

http://stackoverflow.com/questions/19137559/when-hiding-the-statusbar-my-navigation-bar-moves-up-in-ios7

You can create a custom UIView with its frame as

customView.frame=CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height);

Also hide your status bar by following the below steps

Go to info.plist and add two attributes if not present. set "Status bar is initially hidden" to YES and set UIViewControllerBasedStatusBarAppearance to NO. This will hide status bar for your app.

 

 

再配置一个 plist 属性

 

 

 

 

其他方法

Add this code in your view Controller:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

 

I had to do this once.. I ended up creating a custom navigation bar of my own and then just set the frame as:

navBar.frame=CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height);

It worked for me at the time,…just try it out.

posted on 2015-03-03 11:26  奋进的闹钟  阅读(369)  评论(0编辑  收藏  举报

导航