Status bar and navigation bar appear over my view's bounds in iOS 7
2014-08-07 18:02
615 查看
186down votefavorite 122 | I recently downloaded Xcode 5 DP to test my apps in iOS 7. The first thing I noticed and confirmed is that my view's bounds is not always resized to account for the status bar and navigation bar. In viewDidLayoutSubviews, I print the view's bounds: {{0, 0}, {320, 568}} This results in my content appearing below the navigation bar and status bar. I know I could account for the height myself by getting the main screen's height, subtracting the status bar's height and navigation bar's height, but that seems like unnecessary extra work. Has anyone else experienced this issue? UPDATE: I've found a solution for this specific problem. Set the navigation bar's translucent property to NO: self.navigationController.navigationBar.translucent = NO; This will fix the view from being framed underneath the navigation bar and status bar. However, I have not found a fix for the case when you want the navigation bar to be translucent. For instance, viewing a photo full screen, I wish to have the navigation bar translucent, and the view to be framed underneath it. That works, but when I toggle showing/hiding the navigation bar, I've experienced even stranger results. The first subview (a UIScrollView) gets its bounds y origin changed every time. ios ios7
| ||||||||||||||||||||||||||||||||
/ show 7 more comments | |||||||||||||||||||||||||||||||||
start a bounty |
protected by 0x7fffffff Sep 13 at 12:17
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on thissite.
11 Answers
activeoldestvotesup vote192down voteaccepted | You can achieve this by implementing new property called edgesForExtendedLayoutin iOS7 SDK. Please add the following code to achieve this, if([self respondsToSelector:@selector(edgesForExtendedLayout)])self.edgesForExtendedLayout =UIRectEdgeNone; You need add the above in your -(void)viewDidLoadmethod. iOS 7 brings several changes to how you layout and customize the appearance of your UI. The changes in view-controller layout, tint color, and font affect all the UIKit objects in your app. In addition, enhancements to gesture recognizer APIs give you finer grained control over gesture interactions. Using View Controllers In iOS 7, view controllers use full-screen layout. At the same time, iOS 7 gives you more granular control over the way a view controller lays out its views. In particular, the concept of full-screen layout has been refined to let a view controller specify the layout of each edge of its view. The wantsFullScreenLayoutview controller property is deprecated in iOS 7. If you currently specify wantsFullScreenLayout = NO, the view controller may display its content at an unexpected screen location when it runs in iOS 7. To adjust how a view controller lays out its views, UIViewControllerprovides the following properties: edgesForExtendedLayout The edgesForExtendedLayoutproperty uses the UIRectEdgetype, which specifies each of a rectangle’s four edges, in addition to specifying none and all. Use edgesForExtendedLayoutto specify which edges of a view should be extended, regardless of bar translucency. By default, the value of this property is UIRectEdgeAll. extendedLayoutIncludesOpaqueBars If your design uses opaque bars, refine edgesForExtendedLayoutby also setting the extendedLayoutIncludesOpaqueBarsproperty to NO. (The default value of extendedLayoutIncludesOpaqueBarsis NO.) automaticallyAdjustsScrollViewInsets If you don’t want a scroll view’s content insets to be automatically adjusted, set automaticallyAdjustsScrollViewInsetsto NO. (The default value of automaticallyAdjustsScrollViewInsetsis YES.) topLayoutGuide, bottomLayoutGuide The topLayoutGuideand bottomLayoutGuideproperties indicate the location of the top or bottom bar edges in a view controller’s view. If bars should overlap the top or bottom of a view, you can use Interface Builder to position the view relative to the bar by creating constraints to the bottom of topLayoutGuideor to the top of bottomLayoutGuide. (If no bars should overlap the view, the bottom of topLayoutGuideis the same as the top of the view and the top of bottomLayoutGuideis the same as the bottom of the view.) Both properties are lazily created when requested. Please refer, apple doc
| ||||||||||||||||||||||||||||||||
/ show 6 more comments |
up vote36down vote | You don't have to calculate how far to shift everything down, there's a build in property for this. In Interface Builder, highlight your view controller, and then navigate to the attributes inspector. Here you'll see some check boxes next to the words "Extend Edges". As you can see, in the first screenshot, the default selection is for content to appear under top and bottom bars, but not under opaque bars, which is why setting the bar style to not translucent worked for you. As you can somewhat see in the first screenshot, there are two UI elements hiding below the navigation bar. (I've enabled wireframes in IB to illustrate this) These elements, a UIButton and a UISegmentedControl both have their "y" origin set to zero, and the view controller is set to allow content below the top bar. This second screenshot shows what happens when you deselect the "Under Top Bars" check box. As you can see, the view controllers view has been shifted down appropriately for its y origin to be right underneath the navigation bar. This can also be accomplished programmatically through the usage of -[UIViewController edgesForExtendedLayout]. Here's a link to the class reference for edgeForExtendedLayout, and forUIRectEdge [self setEdgesForExtendedLayout:UIRectEdgeNone];
| ||||||||||||||||||||
comment |
up vote22down vote | I created my view programmatically and this ended up working for me:-(void) viewDidLayoutSubviews {// only works for iOS 7+if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){CGRect viewBounds =self.view.bounds;CGFloat topBarOffset =self.topLayoutGuide.length;// snaps the view under the status bar (iOS 6 style) viewBounds.origin.y = topBarOffset *-1;// shrink the bounds of your view to compensate for the offset viewBounds.size.height = viewBounds.size.height +(topBarOffset *-1);self.view.bounds = viewBounds;}} Source (in topLayoutGuide section at bottom of pg.39).
| ||||||||||||||||||||||||||||||||
/ show 7 more comments |
up vote8down vote | If you want the view to have the translucent nav bar (which is kind of nice) you have to setup a contentInset or similar. Here is how I do it: // Check if we are running on ios7if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue]>=7){CGRect statusBarViewRect =[[UIApplication sharedApplication] statusBarFrame];float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height; myContentView.contentInset =UIEdgeInsetsMake(heightPadding,0.0,0.0,0.0);}
| ||||||||||||||||||||||||||||||||
/ show 2 more comments |
up vote6down vote | In your apps plist file add a row call it "View controller-based status bar appearance" and set it to NO
| ||||||||||||||||||||
comment |
up vote5down vote | The simplest trick is to open the NIB file and do these two simple steps: Just togle that and set it to the one you prefer. Select those UIView's/UIIMageView's/... that you want to be moved down. In my case only the logo was overlapped an I've set the delta to +15; (OR -15 if you chose iOS7 in step 1) And THE RESULT:
| ||||||||||||||
comment |
up vote5down vote | edgesForExtendedLayoutdoes the trick for iOS 7. However, if you build the app across iOS 7 SDK and deploy it in iOS 6, the navigation bar appears translucent and the views go beneath it. So, to fix it for both iOS 7 as well as for iOS 6 do this: self.navigationController.navigationBar.barStyle =UIBarStyleBlackOpaque;if([self respondsToSelector:@selector(edgesForExtendedLayout)])self.edgesForExtendedLayout =UIRectEdgeNone;// iOS 7 specific
| ||
add comment |
up vote3down vote | add the key View Controller-based status bar appearancefrom the dropdownlist as a row in info.plistsomething like this :
| ||||||||||||||
comment |
up vote3down vote | I would like to expand on Stunner's answer and add an ifstatement to check if it is iOS-7 because when I tested in iOS-6 my app would crash. The addition would be adding: if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) So I would suggest adding this method to your MyViewControler.mfile: -(void) viewDidLayoutSubviews {if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){CGRect viewBounds =self.view.bounds;CGFloat topBarOffset =self.topLayoutGuide.length; viewBounds.origin.y = topBarOffset *-1;self.view.bounds = viewBounds;}}
| ||
add comment |
up vote2down vote | To me, the simplest solution is to add two keys into the plist
| ||||||||
comment |
up vote1down vote |
相关文章推荐
- Customizing Navigation Bar and Status Bar in iOS 7
- Customizing Navigation Bar and Status Bar in iOS 7
- Customizing Navigation Bar and Status Bar in iOS 7
- Customizing Navigation Bar and Status Bar in iOS 7 .
- Customizing Navigation Bar and Status Bar in iOS 7
- iOS Newbie - Xcode 7 & iOS 9 Navigation Bar & Status Bar
- IOS删除tableview'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in
- [问题记录]'dismissModalViewControllerAnimated:' is deprecated: first deprecated in iOS 6.0
- IOS学习-报错误 Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
- Change Xamarin iOS navigation and status bar color
- iOS: subview fade in and out
- warning D9035 : option 'Wp64' has been deprecated and will be removed in a future
- different addSubview behavior between iOS 4.3 and 5.0(viewWillAppear 不被调用)
- Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
- (转)A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'
- OGG-00665 (status = 1502-ORA-01502: index 'index' or partition of such index is in unusable state)
- IOS成长之路-code signing is required for product type 'Application' in SDK 'iOS 6.0'
- cvc-complex-type.3.2.2: Attribute 'singleton' is not allowed to appear in element 'bean
- 错误整理:No plugin found for prefix 'jetty' in the current project and in the plugin groups
- cvc-complex-type.3.2.2: Attribute 'singleton' is not allowed to appear in element 'bean'