Add Uiview Banner Above Status Bar iOS 7

Add UIView banner above status bar iOS 7

Create a new window and add your banner view to that window. When you need to show the banner, you can set yourwindow.hidden = NO; You can further add animations to showing it and to dismiss it yourwindow.hidden = YES;.

The key here is is setting yourwindow.windowLevel = UIWindowLevelStatusBar+1;

That will make sure your banner view and the yourwindow always appear above the status bar.

Feel free to ask questions regarding any of the above.

Display UIView Above Apple Status Bar in iOS 8

EDIT (November 16, 2015)

I am copying my answer from this thread below:

This answer breaks in iOS 8.3+ and possibly previous versions of iOS 8. I DO NOT recommend that anyone uses it, especially since it is not guaranteed to work in future iOS versions.

My new solution uses the standard presentViewController method of a UIViewController. I initialize a UIViewController, add my custom modal View as a subview of the UIViewController, and then optionally position the modal View using constraints. Works like a charm!


Original Answer

Following Anna's advice, I got it working by using a UIWindow instead of a UIView. Here's my new code:

var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)

modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)

modalWindow.addSubview(customView)

modalWindow.makeKeyAndVisible()

It now works exactly as before, except the text on the Apple status bar gets dimmed as well. Thank you very much for all of the help!

Is there any way to cover an iOS 7 status bar with a UIView temporarily?

You can do it wizh the real Statusbar. Just get the Statusbars UIView you then need to play with the UIWindowLevel like this

    //Getting the Statusbar

UIView *statusbar;
NSString *key = @"statusBar";
id object = [UIApplication sharedApplication];
if ([object respondsToSelector:NSSelectorFromString(key)]) {
statusbar = [object valueForKey:key];
}

//Set your Overlapping UIViewController or UIView one Level higher then the Statusbar.
self.navigationController.view.window.windowLevel = UIWindowLevelStatusBar+1; //This will set the Overlapping UIViewControllers WindowLevel over the StatusBar.

Display UIView Above Apple Status Bar in iOS 8

EDIT (November 16, 2015)

I am copying my answer from this thread below:

This answer breaks in iOS 8.3+ and possibly previous versions of iOS 8. I DO NOT recommend that anyone uses it, especially since it is not guaranteed to work in future iOS versions.

My new solution uses the standard presentViewController method of a UIViewController. I initialize a UIViewController, add my custom modal View as a subview of the UIViewController, and then optionally position the modal View using constraints. Works like a charm!


Original Answer

Following Anna's advice, I got it working by using a UIWindow instead of a UIView. Here's my new code:

var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)

modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)

modalWindow.addSubview(customView)

modalWindow.makeKeyAndVisible()

It now works exactly as before, except the text on the Apple status bar gets dimmed as well. Thank you very much for all of the help!

Status bar and navigation bar appear over my view's bounds in iOS 7

You can achieve this by implementing a new property called edgesForExtendedLayout in iOS7 SDK. Please add the following code to achieve this,

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

You need to add the above in your -(void)viewDidLoad method.

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 wantsFullScreenLayout view 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, UIViewController
provides the following properties:

  • edgesForExtendedLayout

The edgesForExtendedLayout property uses the UIRectEdge type,
which specifies each of a rectangle’s four edges, in addition to
specifying none and all. Use edgesForExtendedLayout to 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 edgesForExtendedLayout by
also setting the extendedLayoutIncludesOpaqueBars property to
NO. (The default value of extendedLayoutIncludesOpaqueBars is NO.)

  • automaticallyAdjustsScrollViewInsets

If you don’t want a scroll view’s content insets to be automatically
adjusted, set automaticallyAdjustsScrollViewInsets to NO. (The
default value of automaticallyAdjustsScrollViewInsets is YES.)

  • topLayoutGuide, bottomLayoutGuide

The topLayoutGuide and bottomLayoutGuide properties 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 topLayoutGuide or to the top of
bottomLayoutGuide. (If no bars should overlap the view, the bottom of
topLayoutGuide is the same as the top of the view and the top of
bottomLayoutGuide is the same as the bottom of the view.) Both
properties are lazily created when requested.

Please refer, apple doc

iOS 7 status bar like iOS 6

1) It's a hack, but it works!

Use it if you doesn't use UIAlertView or KGStatusBar!!!

#import <objc/runtime.h>

@interface UIScreen (I_love_ios_7)
- (CGRect)bounds2;
- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation;
@end

@implementation UIScreen (I_love_ios_7)
- (CGRect)bounds2
{
return [self boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}

- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation
{
CGRect resultFrame = [self bounds2];
if(UIInterfaceOrientationIsLandscape(orientation))
resultFrame.size.width -= 20;
else
resultFrame.size.height -= 20;
return resultFrame;
}
@end

void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
Swizzle([UIScreen class], @selector(bounds2), @selector(bounds));
[application setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.clipsToBounds =YES;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationWillChangeStatusBarOrientationNotification
object:nil];
NSDictionary* userInfo = @{UIApplicationStatusBarOrientationUserInfoKey : @([[UIApplication sharedApplication] statusBarOrientation])};
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillChangeStatusBarOrientationNotification
object:nil
userInfo:userInfo];
}

return YES;
}

- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
CGSize size = [[UIScreen mainScreen] boundsForOrientation:orientation].size;
int w = size.width;
int h = size.height;
float statusHeight = 20.0;
switch(orientation){
case UIInterfaceOrientationPortrait:
self.window.frame = CGRectMake(0,statusHeight,w,h);
break;
case UIInterfaceOrientationPortraitUpsideDown:
self.window.frame = CGRectMake(0,0,w,h);
break;
case UIInterfaceOrientationLandscapeLeft:
self.window.frame = CGRectMake(statusHeight,0,w,h);
break;
case UIInterfaceOrientationLandscapeRight:
self.window.frame = CGRectMake(0,0,w,h);
break;
}
}
@end

2) Create category, and always use contentView instead of view

@interface UIViewController(iOS7_Fix)
@property (nonatomic, readonly) UIView* contentView;
- (void)updateViewIfIOS_7;
@end

@implementation UIViewController(iOS7_Fix)
static char defaultHashKey;
- (UIView *)contentView
{
return objc_getAssociatedObject(self, &defaultHashKey)?: self.view;
}

- (void)setContentView:(UIView *)val
{
objc_setAssociatedObject(self, &defaultHashKey, val, OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;
}

- (void)updateViewIfIOS_7
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7 || objc_getAssociatedObject(self, &defaultHashKey))
return;

UIView* exchangeView = [[UIView alloc] initWithFrame:self.view.bounds];
exchangeView.autoresizingMask = self.view.autoresizingMask;
exchangeView.backgroundColor = [UIColor blackColor];

UIView* view = self.view;
if(self.view.superview){
[view.superview addSubview:exchangeView];
[view removeFromSuperview];
}
[exchangeView addSubview:view];
self.view = exchangeView;

CGRect frame = self.view.bounds;
frame.origin.y += 20.0;
frame.size.height -= 20.0;
view.frame = frame;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self setContentView:view];
}

In every UIViewController:

- (void)viewDidLoad
{
[super viewDidLoad];
[self updateViewIfIOS_7];
UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
lab.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:lab];
//...
}

Portrait
Landscape

Display UIView Above Apple Status Bar in iOS 8

EDIT (November 16, 2015)

I am copying my answer from this thread below:

This answer breaks in iOS 8.3+ and possibly previous versions of iOS 8. I DO NOT recommend that anyone uses it, especially since it is not guaranteed to work in future iOS versions.

My new solution uses the standard presentViewController method of a UIViewController. I initialize a UIViewController, add my custom modal View as a subview of the UIViewController, and then optionally position the modal View using constraints. Works like a charm!


Original Answer

Following Anna's advice, I got it working by using a UIWindow instead of a UIView. Here's my new code:

var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)

modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)

modalWindow.addSubview(customView)

modalWindow.makeKeyAndVisible()

It now works exactly as before, except the text on the Apple status bar gets dimmed as well. Thank you very much for all of the help!

Adding a UIView to a TableViewControllers view

how do I create a view inside this tableViewController that is more like a sibling to the tableView rather than a child that moves with it?

Unfortunately this is not possible with a tableviewcontroller, because the view of a tableviewcontroller is a UITableView instance, which in turn is a subclass of UIScrollView. And basically anything you put as a child of a scrollview scrolls with it.

A better approach would be to move away from using a tableviewcontroller, and use a normal view controller with an embedded table view instead. You can even reuse your view controller by just adding it to the heirarchy.

Another (easier) approach is to handle the scroll delegate method of the uitableview, and adjust the header view's Y position according to the offset.



Related Topics



Leave a reply



Submit