UIBarButtonItem not shown in subview - iphone

I want to create a subview with a NavigationBar and a TableView. In the Navigation Bar should be a button which can edit the tableView. The navigationBar and the TableView is shown in the subview but the barButtonItem not. Is there an error in my code, or is there a problem because it is a subview ?
UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(0, 325, screenSize.size.width, 200)];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(-10, 40, screenSize.size.width, 150) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[tagView addSubview:tableView];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, tagView.frame.size.width, 45)];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[tagView addSubview:navigationBar];
[self.view addSubview:tagInstruction];
[self.view addSubview:tagView];

self.navigationItem refer to navigationController navigation item and you don't have any navigation controller, you have only custom NavigationBar and hence its not working.
For solving this problem you have to create your own UIButton and add in the subview of NavigationBar.

The navigationItem represents the view controller in a parent’s navigation bar, not in the navigationBar you are constructing here. Put the Button into your navigationBar.

If I need a clean navigationBar I always put my UIViewControllers in a UINavigationController. It does not hurt and you can add navigation functionality later. Then you can use self.navigationItem to add a UIBarButtonItem

Like Sven said, the self.navigationItem refers to the UINavigationItem for the UINavigationBar which belongs to the view controller's parent UINavigationController. If you want to set your button in the navigation bar you just alloc'd, try this:
UINavigationItem * navigationItem = [[UINavigationItem alloc] init];
navigationItem.leftBarButtonItem = self.editButtonItem;
[navigationBar setItems:#[navigationItem]];

Related

How to make Main VC's Toolbar global for all view controller

- (void)viewDidLoad{
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"ka1_046.png"]];
myImage.frame = CGRectMake(0, 0, 320, 460);
[baseView addSubview:myImage];
// create the UIToolbar at the bottom of the view controller
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 425, 320, 40)];
toolbar.barStyle = UIBarStyleBlackOpaque;
[baseView addSubview:toolbar];
How can i make Main VC's UIToolbar which has no navigation controller and no tabbarcontroller global for all VC's
There are several ways,
First: you could instead of presenting the new viewcontrollers, you could just add them to the self.view and minimize their size to fit the view controller minus the tabbar
Second: you could share the instance of the tab bar in some global class, such as app delegate, just create the tab bar in the app delegate and add a property around it, so that other VC can access it, and then at each viewDidLoad of a new VC you would add it

Rotate UIBarButtonItem

I want to rotate a UIBarButtonItem.
I have been able to do this with UIButtons using setTransform:CGAffineTransformMakeRotation(…), but UIBarButtonItem does not have this property.
I know the Camera-app does this, so it should be achieveable.
How can I achieve this?
Thanks.
Have you tried using a custom view inside the UIBarButtonItem that you then transform any way you want?
UIBarButtonItem does not extend UIView, so it cannot be transformed directly. You can add the UIBarButtonItem you wish to transform to a UIToolbar, transform the UIToolbar and then add the toolbar as a custom view to another UIBarButtonItem. This item can then be set as a navigation item or added to another UIToolbar. However, if you are using a custom view or image then Emil's approach in the comment above is best.
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(handleForwardItemTouch:)];
UIToolbar *backToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
[backToolbar setTransform:CGAffineTransformMakeScale(-1, 1)];
UIBarButtonItem *backToolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:backToolbar] autorelease];
self.navigationItem.rightBarButtonItem = backToolbarItem;
You could put a UIButton inside a Bar Button Item, than rotate the UIButton.
I extended UIToolBar, giving access to its subviews, and in it have a function rotate that rotates the buttons in the opposite direction of the bar:
- (void)rotate: (int)degrees{
//for the bar
self.transform=CGAffineTransformMakeRotation(DegreesToRadian(degrees));
//for the subviews (UIBarButtonItems)
for (UIView * subView in self.subviews)
{
if(!CGRectEqualToRect(subView.bounds, self.bounds))
subView.transform = CGAffineTransformMakeRotation(DegreesToRadian(-degrees));
}
}

How to add tabbar items in navigation controller?

I am working on navigation based apps...firstly when I press alert button..its shows label,button,image and below tabbar items...how can we add tab bar items?
The most multipurpose way is to add transparent UIView and then add anything to it:
UIView *buttonsHandlerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonsHandlerView.backgroundColor = [UIColor clearColor];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonsHandlerView];
[self.navigationItem setRightBarButtonItem:backButtonItem animated:YES];
[backButtonItem release];
//[buttonsHandlerView addSubview:__anything__];
[buttonsHandlerView release];
Or init UIBarButtonItem with title/type and use it as button

how to integrate UINavigation bar with UIBarbuttonitem?

here is my code....?how to integrate UINavigation bar with UIBarbuttonitem?
UINavigationBar *nav= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
nav.backgroundColor = [UIColor whitecolor];
//nav.tintColor = [UIColor darkGrayColor];
[self.view addSubview:nav];
[nav release];
Try init your item and then
nav.leftBarButtonItem = barButtonItem;
or
nav.rightBarButtonItem = barButtonItem;
You can only have two items on the navigation bar
Note that UIBarButtonItem buttons are properties of the UINavigationItem, not the UINavigationBar.
For example, when using a UINavigationController, it automatically provides its own UINavigationBar. But each of the views managed by the navigation controller can define a UINavigationItem, which controls the title, left and right buttons, etc.
Update: iOS 5 supports multiple buttons on the same side. From the UINavigationItem docs:
Customizing Views
titleView property
leftBarButtonItems property
leftBarButtonItem property
rightBarButtonItems property
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
So, basically, you would write something like this to add two buttons on the left:
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshPlans:)];
UIBarButtonItem *selectYearBtn = [[UIBarButtonItem alloc] initWithTitle:#"Select Year" style:UIBarButtonSystemItemAction target:self action:#selector(selectYear)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
Unfortunately, I haven't seen a way to do this in the Storyboard. Hope this helps.

Hiding UIToolBar for child views of UITableViewController

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];
}