Two different approaches to subviews - ios

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.

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.

How to add SubView in iOS programmatically?

Intially,I created view called MainView.Inside that View I have place the view called UnitsView.My Problem is: If I want to add the UnitsView over MainView means, Whether I have to use self.view or MainView .
UIView *MainView = [[UIViewalloc]initwithframe: CGRectMake (0,0,self.view.frame.size.width, self.view.frame.size.height) ];
TopView.backgroundColor = [UIColor grayColor];
[self.view addSubview:TopView];
UIView *UnitsView = [[UIView alloc]init];
[self.view (or)MainView addsubView:UnitsView];
UIView *UnitsView = [[UIView alloc]initwithframe: CGRectMake(0,0,width,height)];
[MainView addsubView:UnitsView];
[self.view addSubView:MainView];
To add view above view:-
UIView *UnitsView = [[UIView alloc]initwithframe:CGRectMake(0,0,100,100)];
UnitsView.backgroundColor = [UIColor grayColor];
[self.view addSubView:UnitsView];
UIView *SubUnit = [[UIView alloc]initwithframe:CGRectMake(0,0,50,50)];
SubUnit.backgroundColor = [UIColor greenColor];
[self.UnitsView addSubView:SubUnit];

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 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...

Label not getting printed to ios simulator objective c

I can't seem to print my second UILabel , however the first one gets printed. Any help is appreciated.
I am trying to print the yellow label on top of the blue uiview , like i have successfully done with the red label.
here is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect frame = CGRectMake(10, 30, 100, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
UILabel *myLabel2= [[UILabel alloc] initWithFrame:frame];
myLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
myLabel.backgroundColor = [UIColor redColor];
myLabel.text = #"Hello";
myLabel2=[[UILabel alloc] initWithFrame:CGRectMake(50,300,100,100)];
myLabel2.backgroundColor=[UIColor yellowColor];
myLabel2.text=#"World!";
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 500, 500)];
UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(65,50,200,200)];
UIView *myView2= [[UIView alloc] initWithFrame:CGRectMake(65,260,200,200)];
myView1.backgroundColor=[UIColor greenColor];
myView.backgroundColor=[UIColor cyanColor];
myView2.backgroundColor=[UIColor blueColor];
[myView addSubview:myView1];
[myView addSubview:myView2];
[myView2 addSubview:myLabel2];
[myView1 addSubview:myLabel];
[self.window addSubview:myView];
[self.window makeKeyAndVisible];
return YES;
}
thanks for any help.
"Actually your label is getting drawn outside the view area of myView2."
width and height of myView2 is 200,200.
myLabel2 to has y position of 300 which is larger that its superView(myView2)'s height.
:).
myLabel2 is drawn outside myView2's frame. Always y position of a view is taken relative to its superview.
Change this line
myLabel2=[[UILabel alloc] initWithFrame:CGRectMake(50,300,100,100)];
to
myLabel2=[[UILabel alloc] initWithFrame:CGRectMake(50,30,100,100)];
Reason is that your label2 is going outside your view
Hope this helps you..

Resources