ios 11 custom navbar goes under status bar
Your code was always wrong. You should not be setting the height of a manually added navigation bar yourself or placing it at the top of the view. You should pin the top of the navigation bar to the bottom of the status bar (e.g. the top of the Safe Area) and give it a delegate so that you can use the UIBarPositioningDelegate mechanism to set its position to .topAttached
, which will cause it to stretch up to the top of the screen correctly.
(But you should also be asking yourself why you are adding a navigation bar manually. There is usually no reason not to wrap your view controller in a UINavigationController — even if you don't intend to do any actual navigation — just to get the navigation bar, with all its automatic management.)
iOS 11 UINavigationBar custom background always extends under status bar
I ran into the same issue. Like you, forBarPosition did nothing to fix the issue even though it seems it should.
I would call my solution a work around.
The image I was using was 44px high. Since I knew the status bar was 20px, I created a new image that was 64px high. The effect I was going for included extending the image under the status bar and this is working just like I want it to.
The status bar is transparent so you could make the top 20px black or transparent or whatever you want to get your desired effect.
So far this is working for me in the simulator and iPhone SE device both at iOS 11, and iPod touch device running iOS 9.3.5.
Hope this helps you.
EDIT: One additional point. What also worked for me was using a stretchable image. This filled the entire space without repeating as it does in your example, however, the image I was using cannot be stretched so ultimately this did not work for my scenario.
Navigation Bar goes beneath Status Bar in iOS application
If you want to use navigation controller and tab bar controller together you should embed tab bar in a navigation controller.
Please see below stack post:
Using tabbar and navigation bar together
Navigation Bar colides with status bar in swift xcode for iphone 8 simulator
In your AppDelegate file you can add this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
{
//(by default it is True)
UINavigationBar.appearance().isTranslucent = false
// it will prevent overlaping
}
Apple Docs
OR you can add this code in your viewController
self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
Safe area insets change when hiding status bar iOS 11
Constraints that are set to the safe area are affected by the status bar as well as the views actual location on the screen and its transform. If you always want to just apply the top (or bottom) safe area height to your view constraint, you can do this by use of a custom constraint instead.
The following constraint will automatically set its constant
value to the height of the device's top safe area height, not affected by the status bar or other parameters. To use it, change the class of any constraint into this, and their constant
will always be the safe area height. Note that it will not change its value when the device is rotated.
Objective-C
@interface TopSafeAreaContraint : NSLayoutConstraint
@end
@implementation TopSafeAreaContraint
- (void)awakeFromNib {
[super awakeFromNib];
if (@available(iOS 11.0, *)) {
UIEdgeInsets insets = [UIApplication sharedApplication].keyWindow.safeAreaInsets;
self.constant = MAX(insets.top, 20.0);
} else {
// Pre-iOS 11.0
self.constant = 20.0;
}
}
@end
Swift
class TopSafeAreaConstraint: NSLayoutConstraint {
override func awakeFromNib() {
super.awakeFromNib()
if #available(iOS 11.0, *) {
let insets = UIApplication.shared.keyWindow?.safeAreaInsets ?? .zero
self.constant = max(insets.top, 20)
} else {
// Pre-iOS 11.0
self.constant = 20.0
}
}
}
ios 11 transparent navigation bar
old:
if you have used tableView,add code:
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever
} else {
self.automaticallyAdjustsScrollViewInsets = NO
}
new:
a change of automaticallyAdjustsScrollViewInsets in iOS11:
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets
API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's
contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0));
// Defaults to YES
about contentInsetAdjustmentBehavior:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewContentInset = YES inside a navigation controller, regardless of whether the scroll view is scrollable
UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
*/
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
it could be a problem of safeArea for iOS11.
try this define from one expert:
#define adjustsScrollViewInsets_NO(scrollView,vc)\
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
[scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\
} else {\
vc.automaticallyAdjustsScrollViewInsets = NO;\
}\
_Pragma("clang diagnostic pop") \
} while (0)
Related Topics
Xcode Swift Am/Pm Time to 24 Hour Format
How to Make a Random Color With Swift
When Should I Access Properties With Self in Swift
Use Binding≪Int≫ With a Textfield Swiftui
Check If 'Any' Value Is Object
Value of Type 'Storagemetadata' Has No Member 'Downloadurl'
Swift - Spritekit Cgpoint Alignment
What Is Geometry Reader in Swiftui
Getting "File Not Found" in Bridging Header When Importing Objective-C Frameworks into Swift Project
Can You Execute an Applescript Script from a Swift Application
What's the Simplest Way to Convert from a Single Character String to an Ascii Value in Swift
Prevent Dismissal of Uialertcontroller
How to Create Local Scopes in Swift
Swiftui Iterating Through Dictionary With Foreach
Get Terminal Output After a Command Swift
Swiftui 2: the Way to Open View in New Window
How to Make a Custom Initializer For a Uiviewcontroller Subclass in Swift