(void)viewDidLoad {
[super viewDidLoad];
UINavigationController *naviController = [[UINavigationController alloc]init];
[self.view addSubview:naviController.view];
}
If I add navigation controller in the view, it appears about 20 pixels below status bar. I want it appears just below status bar. How do I fix this?
Just set the frame for naviController.view
naviController.view.frame = self.view.frame;
Assuming that you're adding your navigation bar at 0,0 then it looks like you're view isn't positioning correctly.
The easy fix is to move your bar to be at 0,-20
UINavigationController *naviController = [[UINavigationController alloc]init];
CGRect frame = [naviController frame];
frame.origin.y = -20;
[naviController setFrame:frame];
[self.view addSubview:naviController.view];
However, that's a hack.
Try something like this instead :
[[self view] setAutoresizesSubviews:YES];
UINavigationController *naviController = [[UINavigationController alloc]init];
[naviController setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin];
[self.view addSubview:naviController.view];
That might work?
DeclareUINavigationController in the app delegate's applicationDidFinishLaunching:
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:viewControllerOfYourExample animated:YES];
Related
I'm trying to present a view controller with its presentation style set to form sheet. However, when the view controller appears, it is positioned to the right of the screen, not in the center. I am not sure why this is happening, or what I should do. All of these coordinates are correct!
CGSize size = CGSizeMake(650, 550);
//custom init for ViewController so I can position stuff correctly
ViewController *vc = [[ViewController alloc] initWithSize:size];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nav animated:YES];
nav.view.superview.frame = CGRectMake(0, 0, size.width, size.height);
nav.view.superview.center = self.view.center;
It seems that whenever I remove the last two lines, I get what I assume to be the behavior that you are looking for.
CGSize size = CGSizeMake(650, 550);
//custom init for ViewController so I can position stuff correctly
ViewController *vc = [[ViewController alloc] initWithSize:size];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nav animated:YES];
I'm not sure exactly what you were trying to do with those two lines, but without them the view is centered.
in my app from a login screen i am navigating to a class say classA , like this
classA *objUserHome = [[classA alloc]init];
[self presentModalViewController:objUserHome animated:YES];
[objUserHome release];
and ClassA is having a navigating bar and a tabbar(5 tabs in it), i have created my tab bar programmatically like this
- (void)viewDidLoad
{
[super viewDidLoad];
//Create tab bar controller and navigation bar controller
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *arrControllers = [[NSMutableArray alloc] initWithCapacity:5];
//Add PunchClock to tab View Controller
PunchClock* objPunchClock = [[PunchClock alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objPunchClock];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objPunchClock release];
//Add Time_Sheet to tab View Controller
Time_Sheet* objTime_Sheet = [[Time_Sheet alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objTime_Sheet];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objTime_Sheet release];
//Add PTO to tab View Controller
PTO* objPTO = [[PTO alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objPTO];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objPTO release];
//Add PunchClock to tab View Controller
CrewPunch* objCrewPunch = [[CrewPunch alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objCrewPunch];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objCrewPunch release];
//Add PunchClock to tab View Controller
Reports* objReports = [[Reports alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objReports];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objReports release];
// Add this view controller array into the tab bar
//self .viewControllers = arrControllers;
tabBarController .viewControllers = arrControllers;
[arrControllers release];
[self.view addSubview:[tabBarController view]];
}
And ClassA is inherited from UIViewController
now the problem is, after navigating to classA , view of classA is shifted some 4mm downwards why its so?? how can i fix this,,pls help me out ,, thanx in advance
When using storyboards and Modal Transition between 2 or more views you can encounter an error similar to the above.
If you use Modal Transition from ViewControllerA to ViewControllerZ and then attempt to Modal Transition from ViewControllerZ back to ViewControllerA sometimes the view of ViewControllerA gets pushed down the Window slightly.
This can be prevented using:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
On ViewControllerZ to go back to ViewControllerA from an event on ViewControllerZ
You might have chosen some top bar in your Interface Builder or XIB file and additionally set the navigation bar. Dont choose any top bar in the XIB file.
Try as below
[self.navigationController.view addSubview:[tabBarController view]];
After a long research i finally fixed this issue just by inheriting the class from UINavigationController instead of UIViewControler
I managed to successfully change some of my UIView background to a custom image using
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myCustomBackground.jpg"]];
But it does not work on a particular view in a controller that is presented modally as such,
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
navController.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:navController animated:NO];
[loginViewController release];
[navController release];
It seems like self.view inside this controller is behind the view it was showing.
EDIT
LoginViewController.m
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myCustomBackground.jpg"]];
}
self.view in this case seems to be hidden behind the current view that it's displaying. I did not add any subView beforehand.
As you mentioned in your comments, your view contains UIScrollView, UILabel and UIButton.
You should set to all this subviews property backgroundColor = [UIColor clearColor];. Hopefully that would help.
If you are editing subview in IB then you should find property Background in Attributes Inspector and select Clear Color (for all subviews).
How to add navigation bar on to ViewBased application [templet]
The following code will present a new navigation controller.
MyViewController *v = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *t = [[UINavigationController alloc] initWithRootViewController:v];
[self presentModalViewController:t animated:true];
[t release];
[v release];
Otherwise, u can add the navigation bar to the nib file associated with the viewcontroller directly.
In your AppDelegate.m-file, add this under applicationDidFinishLaunching (or something like that).
UINavigationBar *navigationBar = [[UINavigationBar alloc] init];
[window addSubview:navigationBar];
I used the codes below to display an view(ViewController) on window.
ViewImagesController *vvViewImagesController=[[ViewImagesController alloc] init ];
self.vViewImagesController=vvViewImagesController;
[vvViewImagesController release];
UINavigationController *a=[[UINavigationController alloc]initWithRootViewController: vViewImagesController];
[self.view addSubview:a.view];
but the navigation bar appeared underneath the status bar 20 points.
Even I use the codes:
[a.view setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
to relocate the view, but the result is same.
Welcome any comment
Here's what you need to do. It's 1 line and it works:
In viewDidLoad in vvViewImagesController:
[self.view setFrame: [self.view bounds]];
Whoosh!
try instead
ViewImagesController *vvViewImagesController = [[ViewImagesController alloc] init];
UINavigationController *a = [[UINavigationController alloc] initWithRootViewController:vvViewImagesController];
[self presentModalViewController:a animated:YES];
[vvViewImagesController release];
[a release];