add UIView loading screen on top window of application in objective c - ios

I am trying to add an UIView with a UIActivityIndicatorView does not seem to work. Any help appreciated.
UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlayView.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1];
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = overlayView.center;
[overlayView addSubview:activityIndicator];
[activityIndicator startAnimating];
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
[keyWindow addSubview:overlayView];
Trying also adding it like this
[self.navigationController.view addSubview:overlayView];
[self.navigationController.view bringSubviewToFront:overlayView];
seems to work but the navigationbar is not getting hidden

It is possible to add an overlay to a current shown controller like this:
- (void)viewDidAppear:(BOOL)animated; {
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
[self.view.window addSubview:overlayView];
}
This overlay should cover all the current window.

In your viewcontroller class, use the following code to display your overlay view.
UIView *overlayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[overlayView setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.7]];
[self.view.window addSubview:overlayView];
The key here is to use viewcontroller's view's window object so that in case of NavigationController or TabBarController, complete view is overlaid along with navigation bar and or tab bar.

Related

Two different approaches to subviews

Regarding subviews in ios, I have a view that covers the whole screen, then I create and import another view which is just a red square on top of it. I was wondering if there is any difference or advantadge between these two approaches:
Approach 1:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.view addSubview:firstView];
and approach 2:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[myView addSubview:firstView];
The only difference being that in the first case I add both views as subviews of the main property view whereas in the second case I add the second view as a subview of the first view. They look the same on screen.
Thanks
The difference in my opinion that on the first approach if you change any of (myView) constraints , firstFrame will not change but in the second approach it will.

when home button pressed view origin changed?

I have this code in viewDidLoad:
[self.navigationController setNavigationBarHidden:YES];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor whiteColor];
//we need to add white background behind the status bar
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
UIView* statusBarWhiteBackGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, statusBarHeight)];
[statusBarWhiteBackGround setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:statusBarWhiteBackGround];
UIView * redBackGroundColor = [[UIView alloc] init];
[redBackGroundColor setBackgroundColor:[UIColor redColor]];
redBackGroundColor.frame =CGRectMake(0,statusBarHeight , self.view.frame.size.width, self.view.frame.size.height-statusBarHeight);
[self.view addSubview:redBackGroundColor];
statusWhiteBackGround view is used to change the color of the status bar.
here is the result of the above code:
but when the app enter background and then back to foreground, the redBackGroundColor view change it's orging.y as you can see in the bellow picture:
what's the problem ?thanks
Create a UIView programatically on method
-(void)viewDidLayoutSubviews{
screen=[[UIScreen mainScreen] bounds];
//Simple view
self.customView = [[CustomAlertOKView alloc]initWithFrame:CGRectMake(0, 20, screen.size.width, screen.size.height)];
[self.view addSubView:customView];
}
or
Create a view using storyboard and set the x as 0, y value as 20,width and height based on the screen size
#IBOutlet UIView *customView;
set the outlet in the storyboard,
Then set the background color of the custom view to redcolor and main view to white color
self.view.backgroundColor = [UIColor whiteColor];
customView.backgroundColor = [UIColor redColor];

iphone - custom UIAlertView block effect

I am making a custom alertView similar like standard alertView in iOS.
for making block effect , I did this
UIView * backgroundView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0.4f;
[view addSubview:backgroundView];
it works similar like showing standard alert.
but It doesn't works in Tabbar's Area.
How do I ?
[Before showing alert View]
[standard alertView Image]
[cutom alertView Image]
Add backgroundView to UIWindow and bring backgroundView to front.
[[[[UIApplication sharedApplication] delegate] window] addSubview:backgroundView]
Make sure that you add backgroundView on the top.
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview: backgroundView];

How can I add UIView object to self.view.window in ios7?

I'd like to add a dark view layer that covers the whole screen.
In order to do that, I added UIView object to window property of UIViewController as shown below.I remember I was able to add a view to window like this in ios6. But, it doesn't work in ios7.
How am I able to make it work?
Thanks in advance!!
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
[self.view.window addSubview:overlayView];
}
Here's one way of doing what you want:
[[UIApplication sharedApplication].keyWindow addSubview:overlayView];
According to the documentation of UIView, the window property is nil if the view has not yet been added to a window which is the case when viewDidLoad is called.
You can do the same thing in viewDidAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated; {
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
[self.view.window addSubview:overlayView];
}
Swift 3
UIApplication.shared.keyWindow?.addSubview(overlayView);
you can try like this
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *overlayView = [[UIView alloc] initWithFrame:self.view.frame];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
overlayView.center = self.view.center
[self.view addSubview:overlayView];
}
i hope will helpful for you...

iOS TabBarController background color

I have a tabbarcontroller that is pushed onto a navigationController. I tried to change the background color of the tab bar however it does not work:
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[Tab1_iPhone alloc] initWithNibName:#"tab1_iPhone" bundle:nil];
viewController2 = [[Tab2_iPhone alloc] initWithNibName:#"tab2_iPhone" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
CGRect frame = CGRectMake(0.0, 0.0, 480, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor blueColor]]; //003366
[v setAlpha:1.0];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController1, viewController2, nil];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController pushViewController:self.tabBarController animated:YES];
[self.window makeKeyAndVisible];
This is the code I saw on a similar post that changes the bg color:
CGRect frame = CGRectMake(0.0, 0.0, 480, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor blueColor]]; //003366
[v setAlpha:1.0];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
Am I doing something wrong?
Thanks
The below code helps you to add custom colors with RGB values to ur tabBar.
self.tabBarController.tabBar.tintColor = [[UIColor alloc] initWithRed:0.00
green:0.62
blue:0.93
alpha:1.0];

Resources