iOS UIViewController layout issue - ios

I have started to develop iOS without interface builder.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *viewController = [[ViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m
- (void)loadView
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 20)];
[label setText:#"ViewController"];
[label setTextAlignment:NSTextAlignmentCenter];
[label setBackgroundColor:[UIColor redColor]];
[self.view addSubview:label];
}
I don't know why, but when I load application, there is space between top ant botton and view. Does anyone has an idea why?

You need to add the Default-568h#2x.png background image to the project.

If your Deployment Target is iOS 7.0 onwards just check this.
self.edgesForExtendedLayout = UIRectEdgeNone;
I hope it will work...

Related

Created TabBar Programmatically and implemented reside menu but its not working

I have created tabBar controller programmatically and i have to add Reside menu..i implemented reside menu and done all process but its not working.
Here is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.tabBarController =[[UITabBarController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[LeftMenuView alloc] init]];
LeftMenuView *leftMenuViewController = [[LeftMenuView alloc] init];
RESideMenu *sideMenuViewController=[[RESideMenu alloc]initWithContentViewController:navigationController leftMenuViewController:leftMenuViewController rightMenuViewController:leftMenuViewController];
sideMenuViewController.backgroundImage = [UIImage imageNamed:#"logo"];
sideMenuViewController.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
sideMenuViewController.delegate = self;
sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
sideMenuViewController.contentViewShadowOpacity = 0.6;
sideMenuViewController.contentViewShadowRadius = 12;
sideMenuViewController.contentViewShadowEnabled = YES;
self.window.rootViewController = sideMenuViewController;
//Initialize View controller and speciality
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
UIViewController *viewconroller6=[[LeftMenuView alloc]init];
[navigationController pushViewController:viewconroller6 animated:YES];
self.tabBarController.viewControllers=[NSArray arrayWithObjects:viewcontroller1,viewcontroller2,viewcontroller3,viewcontroller4,viewcontroller5, nil];
self.window.rootViewController =self.tabBarController;
self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000];
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//HomeView.h
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self){
UIView *singleView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.title=#"Home";
self.tabBarItem.image=[UIImage imageNamed:#"home.png"];
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 50)];
[navbar setTranslucent:YES];
[navbar setBackgroundColor:[UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000]];
[singleView addSubview:navbar];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
[btn addTarget:self action:#selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Menu" forState:UIControlStateNormal];
btn.frame=CGRectMake(20, 25, 40, 40);
[singleView addSubview:btn];
[self.view addSubview:singleView];
}
return self;
}

Mimicking a notification UIVIew with darkened background

I am interested in creating a pop-up notification view similar to the design patterns seen below, where a UIView pops up and the rest of the screen is darkened, and done without using a new modal view. Since this design pattern is so commonly seen, I was wondering if there is a "tried-and-true" way to doing it (or there a widely used open source framework like AFNetworking for downloading/uploading files and images)?
Thanks.
Here's a good start:
PopupView.h
#import <UIKit/UIKit.h>
#interface PopupView : NSObject
+ (void)addSubview:(UIView *)view;
+ (void)show;
+ (void)hide;
#end
PopupView.m:
#import "PopupView.h"
#implementation PopupView
+(UIView *)sharedView {
static UIView *_view = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
_view.backgroundColor = [UIColor blackColor];
_view.alpha = 0.6;
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hide)];
[_view addGestureRecognizer:singleFingerTap];
});
return _view;
}
+ (void)addSubview:(UIView *)view {
UIView *v = [PopupView sharedView];
[v addSubview:view];
}
+ (void)show {
UIView *v = [PopupView sharedView];
v.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
[[appDelegate window] addSubview:v];
}
+ (void)hide {
UIView *v = [PopupView sharedView];
[v removeFromSuperview];
[v.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
}
Usage:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 21)];
l.text = #"Hello";
l.textColor = [UIColor whiteColor];
[PopupView addSubview:l];
[PopupView show];
Or course instead of a label you can put any view(s) you want.

No UIView appearing in the interface

NOTE: Jump to end for answer.
I am new to IOS Programming. How can I add a visible UIView to the following code. So far it just has the nav bar appear, and black space below, no imageView or the square UIView in the code below.
Below is code in my HomeScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Below is code in AppDelegate.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.myViewController = [[HomeScreenViewController alloc] init];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:self.myViewController];
navBar.title = #"Navigation";
self.window.rootViewController = navBar;
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Here is also the interface for appDelegate. This is literally the only code in the proj other than the generated lines upon creating the project in xcode. I also deleted the storyboard file along with its info in the plist file (otherwise the app would crash).
#import <UIKit/UIKit.h>
#import "HomeScreenViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) HomeScreenViewController *myViewController;
#end
EDITAgain:
No Need to Read Comments, just read this:
Yes, viewDidLoad does invoke.
The background changes to red if i set background color.
No I am not using storyboard.
Still unanswered.
http://postimg.org/image/h5ayao2q3/
FINAL EDIT: Finally! problem solved. It was as simple as setting the background colour for the UIView square. D'oh.
- (void)loadView
{
[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Thank you very much everyone for your help. I guess my UIImage is borked, but I can figure that out myself.
Try changing it:
self.myViewController = [[HomeScreenViewController alloc] init];
TO:
self.myViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController" bundle:nil];
And Make sure there are some label or some thing in HomeScreenViewController. Other part of your code looks fine. Hope this helps. :)
First put breakpoint and verify that ViewDidLoad method is getting called.
If not , since you mentioned that you are creating the views programmatically you need to override the - (void)loadView method and create the view for the view controller.
-(void)loadView {
self.view = [[UIView alloc] init];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
}
Changed the line [square setTintColor:[UIColor orangeColor]]; to [square setBackgroundColor:[UIColor orangeColor]];
right click your UIViewController and check view connection is established to the xib or storyboard view.
ViewDidload will not get called unless view loads from storyboard or xib.
loadView method gets called if you are creating UIView programatically
If you are not using story board or XIB then you need to add the following method
-(void)loadView{
//load the views
}
I have two options to solve this problem:
1) If you have XIB file corresponding to the controller:
HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController"] bundle:nil];
1a. Verify the name of your XIB is correct.
1b. Goto XIB file of HomeScreenViewController -> File Owner -> Connection Inspector -> Check the view outlet is connected.
2) If you do it programmatically, add this to the end of your loadView function:
self.view = square;
so the final code should look like:
- (void)loadView
{
//[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // <-- look at here, your view has changed its frame now
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}

how to show the views added in view controller

I had created the the view programatically but it does not display on the screen
and gives a message in the console that
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps
the designated entry point is not set?
Here is my code
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
exmTableViewController *vc =[[exmTableViewController alloc] init];
self.window.rootViewController=vc;
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
*Code In Viewdidload *
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
self.view =lbl;
}
Instead this line self.view =lbl;, use [self.view addSubview:lbl]; to add subview to your view.
try this
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; [self.view addSubview:img];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
Try like this. self.view = lbl; in this line you assign self.view is as label you want to add subview [self.view addSubview:yourSubView]; do like this.
When you are creating View controller from storyboard, view controller is initiated by storyboard. You should not create windows root controller by yourself. You need to provide story board name in Plist.
After loading view viewDidLoad method will be called. Here you need to set up view(Adding and configuring subviews). But you are changing viewControllers view property, that is set by storyboard. If you want to change your view controllers view you can use,
- (void)loadView
// inside this method you can assign new view to view property.
If you are adding view as subview use view's method
- (void)addSubview:(UIView *)view

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