I have UIView controller class where i have programmatically created a UINAvigationController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Hmm";
navigationController = [[UINavigationController alloc] init];
//without this instruction, the tableView appears blocked
navigationController.view.frame = CGRectMake(0, 0, 768, 1004); // <-- nav controller should fill the screen
navigationController.navigationBar.barStyle = UIBarStyleDefault;
navigationController.title = #"Hello";
[navigationController.navigationBar setHidden:NO];
[self.view addSubview:navigationController.view];
CGRect frame = CGRectMake(100, 70, 200, 50);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:#"Bejoy" forState:UIControlStateNormal];
[button addTarget:self action:#selector(myButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[self.view addSubview:button];
}
Now in the button click event
- (void)myButtonClick:(id)sender {
SAPRetailExCustomerSalesOrderDetailViewTVC *customerSalesOrderDetailViewTVC = [[SAPRetailExCustomerSalesOrderDetailViewTVC alloc] initWithNibName:#"SAPRetailExCustomerSalesOrderDetailViewTVC" bundle:nil];
[navigationController pushViewController:customerSalesOrderDetailViewTVC animated:YES];
[customerSalesOrderDetailViewTVC release];
}
Here in the output the view is navigating but the button remains in my view!!!
Also i have to click twice to get the back button in my NavigationController.
Is it because i don't have a title for my navigation bar. How will i set the title here?.
Can someone help me??
Your UIViewcontroller should be the child of the navigation controller and not the other way round. Your UIViewController is not responsible for its own navigation controller.
Use UINavigationController's -initWithRootViewController: where you create your UIViewController object and add the navigation controller's view to the window.
What you are doing is wrong: You add the navigation controller's view to the view controller's one!
Related
Simple question: in the main view controller of my app (which is in a navigation controller), I am customizing the nav bar with something like this:
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"titleImage"]];
UIButton *menuButton = [[UIButton alloc] init];
[menuButton setImage:[UIImage imageNamed:#"menuIcon"] forState:UIControlStateNormal];
[menuButton setFrame:CGRectMake(0, 0, 34, 34)];
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
self.navigationItem.rightBarButtonItem = menuItem;
I want these elements - the title view and the right bar button - to remain consistent throughout the app as I push and pop new view controllers onto and off of my navigation controller.
Of course, I could just set my custom items up in viewDidLoad of every view controller that is pushed onto my navigation stack, but this means that during the animation between two view controllers, my custom items are animated in and out, which is not as clean as I would like.
Any suggestions on how I would go about maintaining those custom elements on my nav bar when switching from vc to vc? Thanks!
You could create your own navigation item class that subclasses UIButton and set all the appearance in that class, then just set your navigation items as those custom UIButtons.
Something like this:
In CustomButton.h
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton
#end
Then in CustomButton.m
#implementation CustomButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Set images etc.
[menuButton setImage:[UIImage imageNamed:#"menuIcon"] forState:UIControlStateNormal];
[menuButton setFrame:CGRectMake(0, 0, 34, 34)];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Then in your viewController:
#import CustomButton.h
- (void)viewDidLoad
{
CustomButton *button = [[CustomButton alloc]init];
self.navigationItem.leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
}
I added toolbar to on screen of navigation based application using the following code
//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
//Add buttons
//load the image
UIImage *buttonImage ;
buttonImage = [UIImage imageNamed:#"test.png"];
//create the button and assign the image for window width and level
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(WWL:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//create a UIBarButtonItem with the button as a custom view
WindowWidthZoom = [[UIBarButtonItem alloc] initWithCustomView:button ];
[items addObject:WindowWidthZoom ];
[self setToolbarItems:items];
[[self navigationController] setToolbarHidden:NO animated:NO];
but when leave the screen , I noticed that the toolbar didn't disappear in the other screens , any suggestion how to hide it before leaving this screen to avoid its presence in other screens , and how to change its color to black
You might add the following to the -viewWillAppear method of all your other view controllers:
[self.navigationController setToolbarHidden:YES animated:NO];
In the view controller where you want the toolbar to appear, make sure you setToolbarHidden to NO also in the -viewWillAppear method. Doing so in the -viewDidLoad method is not enough because this method is not called every time a view appears. For example, when you hit the Back button of a navigation controller and return to a previous view controller, since that view controller has already been loaded, it may not need to load again (and so -viewDidLoad will not be called.)
Please change your last line code:
[[self navigationController] setToolbarHidden:NO animated:NO];
to:
[self setToolbarHidden:NO animated:NO];
And make sure call it in:
- (void)viewDidAppear:(BOOL)animated;
I want to show the subview of a view in popover.
To elaborate,
I have a mainViewController.
I hava a subview 'songListView' in this mainViewController.
I have a button titled 'list' in the mainViewController' itself.
I want to show the songListView in popover on clicking the button 'list'.
So, how should I do this.
You could use the below code as a reference for showing PopOver from a UIButton
-(void) buttonAction:(id)sender {
//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc]
init];
UIView* popoverView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor greenColor];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover =
CGSizeMake(200, 300);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:popoverButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
}
My problem is solved.
I just created another View Controller class, i.e. 'TempPopoverView'.
Then I set the view of this TempPopoverView equal to the subview of my MainView
Here is the code snippet from my code:
TempPopoverView *viewController = [[TempPopoverView alloc]initWithNibName:#"TempPopoverView" bundle:nil];
[self. songListView setHidden:NO];//songListView is subview of MainView
viewController.view=self. songListView;
UINavigationController *navCont = [[UINavigationController alloc]initWithRootViewController:viewController];
navCont.navigationBar.tintColor = [UIColor colorWithRed:.102 green:.102 blue:.102 alpha:1];
[self showPopOverController:navCont andFrame:sender.frame andInView:self.view];//self.view is the MainView
[viewController release];
[navCont release];
U can use presentModalViewController on your main view.
For example
SongListView *songList = [[SongListView alloc] init];
[self presentModalViewController: songList animated: YES];
There are different animations possible as well.
as simple as you think to add subview in self.view
[popoverController.contentViewController.view addSubview:yourselfobject];
In my app there is a background on top of the window:
UIImageView *mybg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"bg_large.png"]];
[window addSubview:mybg];
then a button in top of the background:
UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoButton.frame = CGRectMake(280,420,20,20);
[infoButton addTarget:self action:#selector(showInfoPanel:) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:infoButton];
[infoButton release];
then a scrollview inside a navigation controller, those added to the window on the top:
navController = [[UINavigationController alloc] init];
navController.viewControllers = [[NSArray arrayWithObject:dashboardViewController] retain];
[navController setNavigationBarHidden:YES];
myScrollView.opaque=NO;
myScrollView.backgroundColor = [UIColor clearColor];
dashboardViewController.view.backgroundColor = [UIColor clearColor];
[window addSubview:navController.view];
the scrollview respond to touches as it should, but the infoButton that added before in the top of the background doesn't. I added the infoButton to the top of the bg, to be stable and not scroll with scrollview.
How can I make the button respond to touches, as well as the scrollview together with it?
U are adding UINavigationController.view on the top of button thats why it is not respoiding, Try placing the code [window addSubview:infoButton]; after [window addSubview:navController.view];.
My main controller is a subclass of UITableViewController with a UIToolBar at the bottom and when a row is selected, I'd like to display another view without the toolbar. How can I hide the UIToolBar in the child view? Right now, it's present throughout all child views unless they're created as modal.
Toolbar is created in RootController:
self.toolbar = [[UIToolbar alloc] init];
// add tool bar items here
[self.navigationController.view addSubview:toolbar];
RootController displays its child views as such:
UIViewController *controller = [[UIViewController alloc] init...]
[self.navigationController pushViewController:controller animated:YES];
RootController is instantiated as such in the app delegate's applicationDidFinishLaunching:
RootController *rootcontroller = [[RootController alloc] initWithStyle:UITableViewStyleGrouped];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootcontroller];
[rootcontroller release];
[window addSubview:[self.navigationController view]];
If I add the toolbar to [self.view] within RootController instead of navigationController's view, the toolbar disappears alltogether..
You can try hiding the toolbar before you display our child view with 'toolbar.hidden = YES' and then in your viewWillAppear method, show it again with 'toolbar.hidden = NO'.
Another alternative would be using "removeFromSuperview"
[toolbar removeFromSuperview];
Then use viewDidAppear method in the view where u wanna re-show the toolbar.
It works better than viewWillAppear since the toolbar is added after the view is showed.
(For viewWillAppear, toolbar is added during the transition so it is kinda awkward.)
I got it working with this
[toolbar removeFromSuperview];
Check this
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];
}
- (void) info_clicked:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];
}