How to add a subview to cover UINavigation barin UIViewController? - ios

Hello I want to add a UIViewon top of my current view controller. So I did like this.
- (void)viewDidLayoutSubviews
{
UIView *vwOverlay=[[UIView alloc] initWithFrame:self.view.frame];
[vwOverlay setBackgroundColor:[UIColor blackColor]];
[vwOverlay setAlpha:0.5];
[self.view addSubview:vwOverlay];
}
but this is adding top of my view but behind my navigation bar.Istill can clearly see navigation bar title and navigation menu items and also can click navigation bar items. But I want to cover this navigation bar too from my new view.
How can I do this? Please help me.
Thanks

Use this code
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
CGSize screenBounds = [UIScreen mainScreen].bounds;
UIView *vwOverlay=[[UIView alloc] initWithFrame: screenBounds];
[vwOverlay setBackgroundColor:[UIColor blackColor]];
[vwOverlay setAlpha:0.5];
[window addSubview:vwOverlay];

Related

Navigation Bar is setting tint color when user drag

I'm working on a app where I have a main view controller with a table view on it. And when the user touches an item of the table view, the app segue to a detail view controller.
The detail view controller has got a clear color navigation bar while the main view controller has got a white color navigation bar.
My issue is that when I'm at the detail view controller and I drag the left border of the view and try to swipe left to go back to the main view controller, the navigation bar turns to white color. And if I don't complete the move and stays on the detail view controller, the navigation bar keeps white.
This gif shows the situation better:
So, How can I avoid the navigation bar on turning white color when I try to drag the view?
UPDATE:
On the main view controller:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
On the detail view controller:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.toolbarHidden = YES;
self.navigationController.toolbar.tintColor = [UIColor clearColor];
}
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = YES;
}
You should move what you put in viewDidLoad and in viewWillAppear to viewDidAppear ( in your detail view controller )
In detail view controller: add below code in ViewWillApper methode.
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
bg.backgroundColor = [UIColor yellowColor];
[self.view addSubview:bg];

Show custom view over tab bar in iOS

I want to show custom view over tab bar. I have seen that UIAlertView shows view over UITabBar and user can't interact with tab bar when alertView is open. I tried it by showing it over the keyWindow by using the code below:
[[[UIApplication sharedApplication] keyWindow] addSubview:myViewObject];
but user can still change the tabs.
I used to do it with overlayView when I create custom controls that need to block the block whole window and show
//self refers to active UIViewController
UIView *overlay =[[UIView alloc]initWithFrame:self.view.window.rootViewController.view.bounds];
overlay.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
overlay.tag = 1001;
[overlay setUserInteractionEnabled:YES];
[self.view.window.rootViewController.view addSubview:overlay];
you can add a tap gesture to remove overlayView from superview .
try this out:
UIView *coverView = [[UIView alloc] initWithFrame:self.view.frame];
coverView.backgroundColor = [UIColor greenColor];
coverView.alpha = .3f;
UIWindow *window = [[UIApplication sharedApplication] windows].lastObject;
[window addSubview:coverView];
You can set tabBarController.tabBar.userInteractionEnabled = false on the tab bar while the custom view is shown.

CGRectMake label doesn't appear

I've just started to make a new app with a navigation bar. I made the first view with the navigation in the AppDelegate.m.
AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(2);
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil]];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Then, I making the view of the HomeViewController inside loadView method.
LoadView of HomeViewController :
- (void)loadView
{
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
self.searchPoiInstructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
self.searchPoiInstructionsLabel.layer.borderColor = [UIColor blackColor].CGColor;
self.searchPoiInstructionsLabel.layer.borderWidth = 2.0f;
self.searchPoiInstructionsLabel.text = #"HELLO WORLD";
[self.view addSubview:self.searchPoiInstructionsLabel];
}
My label Hello World doesn't appear...
For the frame, if I set CGRectMake(0, 65, 50, 20) , 65 for y, my "Hello World" appears... (I know, I have to increase the width :) )
I just don't understand the origins of my view... If someone can explain me please.
Thanks
This is iOS7 baggage. If you run your app under iOS6, you will see the label as you expect/intend.
Under iOS7, your view originates under the navigation bar which is why you don't see the label when you set the frame to originate at 0,0. If you look really closely, you can actually see the blurred label behind the Carrier indicator in the status bar.
There are multiple ways to work with this:
If the UINavigationBar is not translucent, then the 0,0 origin will work.
You can update the labels frame using topLayoutGuide
You could use a UIScrollView instead of UIView
Origin in iOS 7 is kind of different. In a Navigation Controller, things below Navigation bar can be showed a little with blur effect. So the label's origin in the Navigation Controller is up-left (0, 0) point below the Navigation bar instead of the leftest point under the Navigation bar. Here a demo for you.
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[[UIDevice currentDevice]systemVersion] componentsSeparatedByString:#"."][0] intValue] >=7) {
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
}
}

Put status bar under view

Here you can see, that the status bar is above the black background. How to put status bar under it? Like you can see the navigation bar is.
I need to get something like this:
this is possible if you create new window and add that view to it.
Make a window property in your ViewController as shown below
#property (nonatomic,strong) UIWindow *window;
And write below code in viewDidLoad
self.window =[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor =[UIColor blueColor];
self.window.alpha =0.5;
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor =[UIColor greenColor];
[self.window addSubview:view];
self.window.hidden =NO;
self.window.windowLevel =UIWindowLevelStatusBar; // it works with window level as UIWindowLevelAlert
[self.window makeKeyAndVisible];
Hope this helps.
Try this code:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
This will hide the status bar.
See the whole post here

How do I add a dark screen that covers all other views (including navigation bar and status bar)?

I want to add a dark screen over all my views, and then present a notification window above it, like in Tweetbot 3:
However, I'm not totally sure how this is accomplished.
Adding a dark view with the size of the screen as its frame does not work, as below: (navigation bar and status bar aren't covered)
UIView *darkOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
darkOverlay.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self.view insertSubview:darkOverlay behindSubview:self.view];
This covers everything except the status bar (so it comes pretty close, but as I have light status bar content it's still very jarring):
[[[UIApplication sharedApplication] keyWindow] addSubview:darkOverlay];
So how is something like this accomplished?
You can try this method
UIWindow* window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.windowLevel = UIWindowLevelAlert;
window.opaque = NO;
[window addSubview:someView];
// window has to be un-hidden on the main thread
[window makeKeyAndVisible];
Where "view" - is your custom popup.
You have to do this:
CGRect screenRect = [[UIScreen mainScreen] bounds];
_darkCoverView = [[UIView alloc] initWithFrame:screenRect];
_darkCoverView.backgroundColor = [UIColor blackColor];
_darkCoverView.alpha = 0.5;
[[[[UIApplication sharedApplication] delegate] window] addSubview:_darkCoverView];
It works for me, and the status bar is covered as well :)
You can try to simply present it modally, as modal view controllers go above nav controllers.
TransparancyViewController *vc=[[TransparancyViewController alloc]initWithNibName:#"TransparancyViewController" bundle:nil] ;
vc.view.backgroundColor = [UIColor clearColor];
navController.view.alpha = 0.3;
navController.modalPresentationStyle = UIModalPresentationCurrentContext;
[navController presentViewController:vc animated:NO completion:nil];
Small sample project for using with Storyboards.. http://cl.ly/442C11341k2G
When you create the darkOverlayView it may be better to specify the actual bounds of the view, which should fill the whole screen (width,height for iPhone 5 used).
UIView *darkOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 568)];
If your notification object is also a UIView, bring this to the front.
[self.view addSubView:darkOvelay]
[self.view bringSubviewToFront:notificationView];
I hope this solves your problem, cheers, Jim.

Resources