iphone - custom UIAlertView block effect - ios

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];

Related

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

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.

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];

How to add a custom UIView to Navigation Bar in iOS7+ like this

Is there a way to insert any UIView in NavigationBar like the image below?
Is this not working?
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
In the code below, I embed a view into the navbar, where my embedded view draws the background and the new buttons. The code also deals with relaying out the view when the device is rotated.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (view.superview == nil) {
[self.navigationBar addSubview:view];
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
}
CGRect frame = self.navigationBar.frame;
frame.size.height = MY_NAVBAR_HEIGHT;
self.navigationBar.frame = frame;
view.frame = self.navigationBar.bounds;
}
An alternative solution is to use a regular UIView as a NavBar, and by saying that what I mean is you have to set the UIView in navbar place and hide the NavigationController bar in the main VC and show it in the next one read this article Here https://medium.com/me/stats/post/54bc5fc56d6c

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 background position

What is the best way to position background image below header image. Currently I have:
header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"h.png"]];
[header setContentMode:UIViewContentModeTop];
[self.window addSubview:header];
background = [[UIImageView alloc] init];
[background setImage:[UIImage imageNamed:#"bg.png"]];
[background setFrameOrigin:CGPointMake(0,header.frameHeight)];
[background addSubview:background];
but I get the following error:
No visible #interface for 'UIImageView' declares the sector 'setFrameOrigin.'
I want to use UIView method, so that I can place a table view inside background view afterwards.
[background addSubview:background];
is not OK in your code. You are trying to add a view as subview to its self.
Just create a rootviewcontroller and set as in your app delegate as rootview controller.
self.window.rootViewController = rootviewcontroller;
then you can add your background view as subview on the view of rootviewcontroller. after that you can do it for headerview.
[rootviewcontroller.view addSubview:background];
[rootviewcontroller.view addSubview: headerview];
you can use:
CGRect rect = background.frame;
rect.oigin.x = x;
rect.origin.y = y;
background.frame = rect;
or:
background = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[background setFrame:CGRectMake(0, header.frame.size.height, 60, 50)];
Try like this it will be helpful to you.

Resources