[英]iOS 7 StatusBar issue when I am using UIImagePickerController Allow editing
I study related iOS 7 status-Bar Maintain, but am I facing an issue while I am using UIImagePickerController
. 我研究相关的iOS 7状态 - Bar Maintain,但我在使用
UIImagePickerController
时遇到了问题。 Allow editing that Editing Window shows 20 pixels space at the top bar. 允许编辑“编辑窗口”在顶部栏显示20像素空间。
I used the code and first I set UIViewControllerBasedStatusBarAppearance
to NO
in info.plist
and set the delegate method: 我使用了代码,首先我在
info.plist
中将UIViewControllerBasedStatusBarAppearance
设置为NO
并设置委托方法:
@property (retain, nonatomic) UIWindow *background;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[application setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.clipsToBounds =YES;
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
background = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
background.backgroundColor =[UIColor redColor];
[background setHidden:NO];
}
In my home view controller that has no effect, so I put in my home viewController, one method for changing the background color of statusbar: 在我的家庭视图控制器中没有任何效果,所以我放入了我的home viewController,一种用于更改状态栏背景颜色的方法:
-(void)viewWillLayoutSubviews{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.view.clipsToBounds = YES;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userIsMale"]) {
[tempWindow setBackgroundColor:[UIColor colorWithRed:(46.0/255.0) green:(134.0/255.0) blue:(255.0/255.0) alpha:1]];
}
else {
[tempWindow setBackgroundColor:[UIColor colorWithRed:(246.0/255.0) green:(26.0/255.0) blue:(113.0/255.0) alpha:1]];
}
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
Now when I present UIImagePickerController
allow editing window that shows it like this: 现在,当我呈现
UIImagePickerController
允许编辑窗口显示如下:
I tried with this solution for hide statusbar while present UIImagePickerController
: 我尝试使用此解决方案来隐藏状态栏,同时呈现
UIImagePickerController
:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
And show status bar code in ViewWillApear
. 并在
ViewWillApear
显示状态条形码。
I got this type of result: 我得到了这种结果:
Where am I doing wrong and how do I solve this? 我在哪里做错了,我该如何解决这个问题?
Have a try with: 试试看:
-(BOOL)prefersStatusBarHidden { return YES; }
It will hide the status bar. 它将隐藏状态栏。
On iOS 7, if you want to use setStatusBarHidden:
, you need to set the View controller-based status bar appearance
as NO in info.plist. 在iOS 7上,如果要使用
setStatusBarHidden:
,则需要在info.plist中将View controller-based status bar appearance
为NO。
I hopes it will give you some hint. 我希望它会给你一些暗示。
Edited: 编辑:
I found the problem. 我发现了这个问题。
This is the log the first time you show the homeViewController at viewWillAppear: 这是第一次在viewWillAppear上显示homeViewController时的日志:
(lldb) po [[UIApplication sharedApplication] windows]
<__NSArrayM 0x8a74560>(
<UIWindow: 0x8e33360; frame = (0 20; 320 548); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x8e348b0>; layer = <UIWindowLayer: 0x8e3c6e0>>,
<UIWindow: 0x8c1db40; frame = (0 0; 320 20); gestureRecognizers = <NSArray: 0x8c1e170>; layer = <UIWindowLayer: 0x8c1da80>>
)
And this is the log after dismissing the imagePicker and call the viewWillAppear: 这是解雇imagePicker并调用viewWillAppear后的日志:
(lldb) po [[UIApplication sharedApplication] windows]
<__NSArrayM 0x8c1a700>(
<UIWindow: 0x8e33360; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x8e348b0>; layer = <UIWindowLayer: 0x8e3c6e0>>,
<UIWindow: 0x8c1db40; frame = (0 0; 320 20); gestureRecognizers = <NSArray: 0x8c1e170>; layer = <UIWindowLayer: 0x8c1da80>>,
<UITextEffectsWindow: 0x8c53380; frame = (0 0; 320 568); hidden = YES; opaque = NO; gestureRecognizers = <NSArray: 0x8c538a0>; layer = <UIWindowLayer: 0x8c53520>>
)
The default window size changed. 默认窗口大小已更改。 That is the reason the status bar can't be shown.
这就是状态栏无法显示的原因。
I edited your code like this, and it works for me: 我编辑了这样的代码,它对我有用:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// [[UIApplication sharedApplication] setStatusBarHidden:YES];
if(OVER_IOS7){
[self.navigationController.navigationBar setTranslucent:NO];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
-(void)viewWillLayoutSubviews{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.view.clipsToBounds = YES;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
self.view.frame = CGRectMake(0, 20, self.view.frame.size.width,screenHeight-20);
self.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UIWindow *defaultWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
defaultWindow.frame = CGRectMake(0,20,320,548);
defaultWindow.bounds = CGRectMake(0,20,320,548);
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"userIsMale"]) {
[tempWindow setBackgroundColor:[UIColor colorWithRed:(46.0/255.0) green:(134.0/255.0) blue:(255.0/255.0) alpha:1]];
}
else {
[tempWindow setBackgroundColor:[UIColor colorWithRed:(246.0/255.0) green:(26.0/255.0) blue:(113.0/255.0) alpha:1]];
}
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
}
- (IBAction)showImagePicker:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[picker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
[self presentViewController:picker animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}];
}
Try to present UIImagePickerController
by: 尝试通过以下方式呈现
UIImagePickerController
:
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
[tempWindow.rootViewController presentViewController:myImgPickerController animated:YES completion:nil];
I don't know if it will help you, but you maybe try it... :) 我不知道它是否会对你有所帮助,但你可能会尝试一下...... :)
Another option is when you present UIImagePickerController
. 另一个选择是当你呈现
UIImagePickerController
。 At that time set your view's frame as it was like at first and also hide status bar... 那时设置你的视图框架就像它一样,并隐藏状态栏...
I also faced the same issue but there is only 1 way to overcome it. 我也面临同样的问题,但只有一种方法可以克服它。
Go to the info.plist file and set the value for "View controller-based status bar appearance" to "NO". 转到info.plist文件并将“查看基于控制器的状态栏外观”的值设置为“NO”。
I got a similar issue, but when I come back from the camera roll to my UIViewController. 我遇到了类似的问题,但是当我从相机卷回到我的UIViewController时。 I solved the problem with the follow code in the controller that pushs the UIImagePickerController:
我用推送UIImagePickerController的控制器中的以下代码解决了这个问题:
First I open the Picker 首先,我打开Picker
- (void)showPhotoPicker:(UIImagePickerControllerSourceType)sourceType
{
[self.picker setSourceType:sourceType];
[self presentViewController:self.picker animated:YES completion:nil];
}
I keep the status bar without any problem, I saw some people have issues with that, I didn't 我保持状态栏没有任何问题,我看到有些人有问题,我没有
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}
It is really important to call view needs layout on completition 在completition上调用视图需求布局非常重要
// Apply photo selected
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.enterPicture setImage:info[UIImagePickerControllerEditedImage]];
[picker dismissViewControllerAnimated:YES completion:^(void){[self.view setNeedsLayout];}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^(void){[self.view setNeedsLayout];}];
}
And here is the magic: 这就是魔术:
// fix the status bar & navigation bar after close the photo album on IOS7
-(void)viewWillLayoutSubviews
{
UINavigationBar *navBar= [[self navigationController] navigationBar];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7 && navBar) {
CGFloat statusBarHeight = [[UIApplication sharedApplication] isStatusBarHidden] ? 0.0f : 20.0f;
[navBar setFrame:CGRectMake(0, statusBarHeight, navBar.frame.size.width, navBar.frame.size.height)];
[self.view setBounds:CGRectMake(0, (statusBarHeight + navBar.frame.size.height) * -1, self.view.bounds.size.width, self.view.bounds.size.height)];
}
}
Just add to controller: 只需添加到控制器:
- (void)viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.