- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Send to Twitter"
style:UIBarButtonItemStyleDone
target:self
action:#selector(save)];
}
return self;
}
Another piece of code
- (void)loadView
{
[super loadView];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor whiteColor];
self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.delegate = self;
textView.font = [UIFont systemFontOfSize:15];
textView.contentInset = UIEdgeInsetsMake(5,5,0,0);
textView.backgroundColor = [UIColor whiteColor];
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
I'm trying to change the navigation bar color to black, however no matter what i do here, the color still stays default (Blue). How do you change the color of the bar on top of the view??
Try something like:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
This should be done on viewWillAppear: as during init method self.navigationController will be obtained as nil and hence will not produce any effect.
Hope this helps.
If you use a programatic approach (seems like you don't) you could write this code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// window initialization
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *viewController = [[YourViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] \
initWithRootViewController:viewController];
[viewController release];
[[navigationController navigationBar] setTintColor:[UIColor blackColor]];
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
It works for me. Hope it helps! ;)
Related
I'm trying to display a UINavigationController inside a UIPopoverController, but for some reason I can't set the title.
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:searchView];
[popover presentPopoverFromBarButtonItem:_searchBarButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
I tried changing it in the - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Change the title
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = #"Print / Email";
[label sizeToFit];
self.navigationItem.titleView = label;
}
return self;
}
I also tried setting it in the - (void)viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
self.contentSizeForViewInPopover = CGSizeMake(320.0, 250.0f);
self.navigationItem.title = #"Print / Email";
self.title = #"Print / Email";
}
But in no cases the title is working, am I still doing something wrong?
EDIT Add your SearchViewController controller in UINavigationController ad that in UIPopoverController like this:
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:searchView];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
[popover presentPopoverFromBarButtonItem:_searchBarButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Now in SearchViewController's viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Print / Email";
}
Refer setting-the-title-of-a-uipopovercontroller
Try with this way to show popover controller
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UINavigationController *navCont=[[UINavigationController alloc] initWithRootViewController:searchView];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navCont];
[popover presentPopoverFromBarButtonItem:_searchBarButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
You can try this:
SearchViewController *searchView = [[[SearchViewController alloc] init] autorelease];
UINavigationController *_nav = [[[UINavigationController alloc] initWithRoot.....:searchView] autorelease];
then put _nav into the popover.
each UIViewController has its own tabbar and navigation but you should init them.then you can use them rightly.
Have a mainviewcontroller and on it have UIToolbar which has a UIBarButtonItem Info which shows UIViewcontroller modally.
Now when pressing Infobutton it is showing UIViewController modally which has UITextView but not showing UINavigationController with Done button.
I am not able to figure it out what i am missing in my code.
This is how i am showing UITextView and NavigationController in UIViewController modally.
#import "ModalViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation ModalViewController
#synthesize textView;
#synthesize navBar;
#synthesize navigationController;
#synthesize delegate;
-(void)dealloc
{
[textView release];
[navBar release];
[navigationController release];
[super dealloc];
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.title = #"Info";
UINavigationController *navigationController = [[UINavigationController alloc]init];
//initWithRootViewController:viewController];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(Done:)] autorelease];
self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]autorelease];
self.textView.textColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Georgia-BoldItalic" size:14];
//self.textView.delegate = self;
self.textView.backgroundColor = [UIColor brownColor];
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor whiteColor] CGColor];
self.textView.layer.cornerRadius = 1;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.
self.textView.editable = NO;
//[self.view addSubview:navigationController.view];
[self.view addSubview: self.textView];
//[navigationController release];
}
And this is how UIViewController presented modally
//Create a final modal view controller
UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self action:#selector(modalViewAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.rightBarButtonItem = modalBarButtonItem;
- (void) modalViewAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//self.viewController = [[ModalViewController alloc] init];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//ModalViewController *myModalViewController = [[ModalViewController alloc] init];
//UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myModalViewController];
//navigationController.navigationBar.tintColor = [UIColor brownColor];
_viewController = [[ModalViewController alloc] init];
//[navigationController pushViewController:_viewController animated:YES];
[self presentModalViewController:self.viewController animated:YES];
//[self.view addSubview:navigationController.view];
//[navigationController release];
[myModalViewController release];
}
I will appreciate if you can figure out what i m doing wrong or missing in my code.
Thanks a lot.
This seems like a needlessly convoluted way of displaying the new controller. In my apps, I do it like this:
//this function displays (modally) view controller nested inside a navcontroller
- (void) showModalController
{
YourViewController * ecreator = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController * navcontrol = [[UINavigationController alloc] initWithRootViewController: ecreator];
[self presentModalViewController: navcontrol animated:YES];
[navcontrol release];
[ecreator release];
}
Now you want to do the graphical setup (navbar color etc) in the initWithNib and/or viewDidLoad functions of the YourViewController.
#synthesize navigationController;
so navigationController is your class member variable.
in the function
- (void) viewDidLoad
you declare a local variable
UINavigationController *navigationController
Please notice that the second navigationController is different from your member variable navigationController .
So, inside viewDidLoad you need to create object of your member variable navigationController. Not the local variable navigationController.
Do not RE-declare navigationController in viewDidLoad. Instead, create the object using member variable like:
navigationController = [[UINavigationController alloc]init];
Try this, it worked great for me. I had the exact same problem
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#“segue_name"])
{
UINavigationController *nav = [segue destinationViewController];
ExampleViewController *exampleVC = (ExampleViewController *) nav.topViewController;
//Setup any properties here and segue
}
}
I'm developing an iOS 4 app using latest SDK and Xcode 4.2.
I've added programmatically a UINavigationController on AppDelegate, and I set it black.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
viewController.title = #"Menu";
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBar.tintColor = [UIColor blackColor];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
On one view controller, I've added a UISegmentedControl:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// Do any additional setup after loading the view from its nib.
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:#"Past"],
[NSString stringWithString:#"Present"],
[NSString stringWithString:#"Future"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentedChanged:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 260, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
self.navigationItem.titleView = segmentedControl;
}
But, when I set one item selected, I can see it because is black.
Is there anyway to set to another color when a item is selected?
Try using the segmentedControlIndexChanged and then detect which one is selected with self.segmentedControl.selectedSegmentIndex and change that segments color.
I have a ViewController with 3 views: Rootview that shows toolbar with UISegmentedControl, tableView, and calendarView.
I have XIB for the rootView, and tableView, but the calendarView doesn't have a XIB.
I need to somehow combine the code to load the calendar view to fit with with this ViewController. Before, I was using the calendarView as its own viewController.
The code for calendarView:
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
calendar = [[TKCalendarMonthView alloc] init];
calendar.delegate = self;
calendar.dataSource = self;
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(dismissCalendarView)];
self.navigationItem.leftBarButtonItem = actionButton;
[actionButton release];
int statusBarHeight = 0;
CGRect applicationFrame = (CGRect)[[UIScreen mainScreen] applicationFrame];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight, applicationFrame.size.width, 300.0)] autorelease];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor clearColor];
self.title = #"Select Workout";
calendar.frame = CGRectMake(0, 0, calendar.frame.size.width, calendar.frame.size.height);
NSLog(#"%f height", applicationFrame.size.height);
[self.view addSubview:calendar];
[calendar reload];
}
If I put that code directly into this new viewController, it does not respect the UISegmentedControl and just shows up at launch.
Here is the code for the UISegmentedConrol:
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
switch (index)
{
case 0:
{
[self.view addSubview: tableView1];
tableView1.hidden = NO;
calendar.hidden = YES;
[calendar removeFromSuperview];
break;
}
case 1:
{
[self.view addSubview: calendar];
tableView1.hidden = YES;
calendar.hidden = NO;
[tableView1 removeFromSuperview];
break;
}
}
}
Does using two different init-Methods fit your needs? initWithNibName:#"nib1" or likewise initWithNibName:#"nib2"??
Otherwise you will need to specify what you want to achieve a little more
I've searched around and I can't seem to figure it out. I'm sure many people will have links for me and such, which I've most likely already looked at. But if someone could please just show me code to do the following:
I'd like to have a left arrow in my UINavigationBar as a "Back" UINavigationItem. How can I do this? Here is my current code in my UIViewController:
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:#selector(backButtonSelected:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Options" style:UIBarButtonItemStyleBordered target:nil action:#selector(optionsButtonSelected:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;
[self.view addSubview:theBar];
I think this may be what you're looking for.
// in .h file
#property (nonatomic, retain) UINavigationBar *navBar;
// in .m file just below #implementation
#synthesize navBar;
// within .m method
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
navBar.barStyle = UIBarStyleBlack;
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:#"Nav Bar Title"];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:#selector(backButtonSelected:)];
title.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:nil
action:#selector(showActionSheet:)];
title.rightBarButtonItem = rightButton;
[navBar pushNavigationItem:title animated:YES];
[self.view addSubview:navBar];
You should use an UINavigationController in order to pop/push UIViewControllers on your screen. The navigation controller will add an UINavigationBar to your UIViewControllers automatically so you will not need to create them as you did.
Here is a sample. I didn't looked for memory leaks.
In the app delegate you'll find this method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
MainVC *mainVC = [[MainVC alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
MainVC is a UIViewController that represents the level 1 of the hierarchy. in it i have
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor redColor];
self.title = #"MainVC";
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"SecondLevel" style:UIBarButtonItemStyleBordered target:self action:#selector(secondLevelSelected:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
- (void) secondLevelSelected:(id)sender
{
SecondVC *secondVC = [[SecondVC alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}
SecondVC is another UIViewController that represents the second level of the hierachy. Here you will find the back button that you want.
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
self.view.backgroundColor = [UIColor greenColor];
self.title = #"SecondVC";
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:#"FirstLevel" style:UIBarButtonItemStyleBordered target:self action:#selector(leftBtnSelected:)];
self.navigationItem.leftBarButtonItem = leftBtn;
}
- (void) leftBtnSelected:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}