toolbar hide behind the navigation image - objective-c

I am creating one app in which i have to use navigation image.
without navigation image i can see the toolbar option perfectly
navigation bar without image at app start up-home screen
navigation image after navigate to second page
now i am adding an image in my navigation bar using
UIImageView *topBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 45)];
topBar.image = [UIImage imageNamed:#"top_cell.png"];
[self.navigationController.navigationBar addSubview:topBar];
now the navigation bar look like at home screen perfect
but in second page the toolbar option hidden
so how can i show toolbar above the image in navigation bar?

Try with
[self.navigationController.navigationBar insertSubview:topBar belowSubview:navigationBar];
It will insert your view under your navigation bar.
A second option is to use this code after yours:
[self.navigationController.navigationBar bringSubviewToFront: topBar]
that will put your top bar at the top of the view stack.

finally lots of work i found the solution first i have added image in viewWillAppear method then after i added my toolbar in viewDidAppear method just like below code
-(void)viewWillAppear:(BOOL)animated
{
// self.navigationController.navigationBarHidden = TRUE;
///// Edit Hiren
UIImageView *topBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 45)];
topBar.image = [UIImage imageNamed:#"top-bar.png"];
[self.navigationController.navigationBar addSubview:topBar];
}
-(void)viewDidAppear:(BOOL)animated
{
toolbar.frame = CGRectMake(0, 0, 685, 44);
// [toolbar sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
// [self.navigationController.view addSubview:toolbar];
}

Related

iOS7 - Navigation Controller - Custom Back Button

I need to use custom Back button image for my new iOS 7 app. When I use Storyboard and add image by using Attributes inspector, this is what XCode renders for me:
Does anyone know why the Navigation controller behaves this way? I'm regularly putting #2x PNG image into Bar Button Item(under Navigation Item) in my Table View.
#interface MEDevicesViewController : UITableViewController
In my application, I use ECSlidingViewController and I have Table View within Navigation Controller. This is on my navigation controller.
Any help appreciated...
UIButton * button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 51, 31)];
[button setImage:[UIImage imageNamed:#"back_btn.png"]forState:UIControlStateNormal];
[button addTarget:self
action:#selector(backbtnpressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
-(IBAction)backbtnpressed:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

How to get rid of the space on the left side of a custom UINavigationItem with a UISearchBar

In my navigation bar, I have a magnifying glass icon that brings up a search bar. I'm not using a UISearchDisplayController, so I opted to build my own UINavigationItem and then push it over the standard UINavigationItem using pushNavigationItem.
The problem is that the UINavigationItem seems to be pushed around 8 pixels to the right. This causes the cancel button (with localized text 'Annuleren') to be really close to the edge of the screen.
I tried inspecting the self.mySearchBar.bounds at runtime, but the origin is 0,0. I've played around a bit with AutoLayout and programmatically added constraints, but I haven't been successful. I hope it's possible without AutoLayout.
This is my code:
- (IBAction)displaySearchBar:(id)sender {
if (!self.mySearchNavigationItem)
{
self.mySearchNavigationItem = [[UINavigationItem alloc] initWithTitle:#""];
self.mySearchNavigationItem.hidesBackButton = YES;
self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.mySearchBar.showsCancelButton = YES;
self.mySearchBar.delegate = self;
[self.mySearchBar sizeToFit];
[self.mySearchBar setPlaceholder:#"Zoeken..."];
UIView *barWrapper = [[UIView alloc]initWithFrame:self.mySearchBar.bounds];
[barWrapper addSubview:self.mySearchBar];
self.mySearchNavigationItem.leftBarButtonItem = nil;
self.mySearchNavigationItem.backBarButtonItem = nil;
self.mySearchNavigationItem.titleView = barWrapper;
UIButton *cancelButton;
UIView *topView = self.mySearchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(#"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
[cancelButton setTitle:#"Annuleren" forState:UIControlStateNormal];
}
}
[self.navigationController.navigationBar pushNavigationItem:self.mySearchNavigationItem animated:YES];
NSTimeInterval delay;
if (self.tableView.contentOffset.y >1000) delay = 0.4;
else delay = 0.1;
[self performSelector:#selector(activateSearch) withObject:nil afterDelay:delay];
}
try:
self.navigationController.navigationBar.barTintColor = self.mySearchBar.barTintColor;
if that doesn't work, you can add an underlay view to the navigation controller that is the color you would like. this may be useful: Get the right color in iOS7 translucent navigation bar
After searching for many hours, I gave up and went for a dirty fix. I'll leave it open for a while, in case someone knows why my searchbar is moved 8 pixels to the right.
Right before showing the UINavigationItem, I move the whole UINavigationBar to x-coordinate -8.
self.navigationController.navigationBar.frame = CGRectMake(-8.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
[self.navigationController.navigationBar pushNavigationItem:self.mySearchNavigationItem animated:YES];
And then on the cancel button click, I move it back to x-coordinate 0.
- (IBAction)cancelSearchBar:(id)sender {
[self.navigationController.navigationBar popNavigationItemAnimated:YES];
self.navigationController.navigationBar.frame = CGRectMake(0.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
}

How to move scrollView down 44x to make room for NavBar

I am using the following code to create my scrollview. I would like to move the scrollView down 44px to make room for my nav bar.
scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor clearColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Menu.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[scroll addGestureRecognizer:doubleTap];
self.view = scroll;
any help is appreciated.
Your code is correct, to manually create a frame do:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,44,320,480)];
:)
This is for iPod and iphone View but this will let the scroll start at 44px
Cant you just add a scroll view in in interface builder as an outlet, place it underneath your nav bar.
Declare scroll view in .h
in .m under ViewDidLoad
[scroll setScrollEnabled:YES];
[scroll setContentSize: CGSizeMake(320, 1000)]; ////or whatever size you want here
You probably want to init it with a different frame... but it depends on where/how this nav bar is being created. If you have a view controller, and these are all subviews of the view controller's view, then you should be creating the objects in viewDidLoad, then just create the nav bar first, using self.view.bounds to obtain the initialization width. I assume you'll want to put UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin as the autoResizingMask here. Then if the scrollview is the rest of the view below the nav bar, you can create the frame for it using
CGRect scrollFrame = CGRectInset(self.view.bounds, 0, navbar.bounds.size.height)
Put an autoresizingMask of UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHeight on the scrollview.
If you are creating the views in a different place/way, then some of that might need modification. I was assuming your view is a nav bar at the top, x pixels tall (44 in this case but it doesn't and shouldn't matter in the context of setting the scrollview frame). and then a scrollview that fills the rest of the view.

Resizing UITabBarController Child Views

I'm sure this is a matter of me not knowing how to phrase my question, but I'm at a loss.
I'm working on an app with 2 subviews that will be on the top of the screen at all times (taking up a total of 114 pixels including the status bar). I want the rest of the space to be taken up with a UITabBar and it's child views.
The closest I've been able to get is a tab view that is sized correctly but is positioned at 0,0 so it's under my persistent subviews.
// Create the main toolbar
toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0.0, 20, 768, 44);
UILabel *textLabel = [[UILabel alloc] init];
textLabel.frame = CGRectMake(240, 20, 300, 40);
textLabel.textAlignment = UITextAlignmentCenter;
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = #"Character Name";
[self.window addSubview:textLabel];
[textLabel release];
...
// Create the stats bar
StatsViewController *statsView = [[StatsViewController alloc] init];
statsView.view.frame = CGRectMake(0.0, 64, 768, 50);
[self.window addSubview:statsView.view];
// Create Tab Bar Controller and Tab View Controllers
tabBarController = [[UITabBarController alloc] init];
...
[self.window setRootViewController:tabBarController];
[tabBarController release];
[self.window addSubview:toolbar];
[self.window bringSubviewToFront:statsView.view];
[self.window bringSubviewToFront:textLabel];
[self.window makeKeyAndVisible];
return YES;
A tab bar controller (or any view controller, for that matter) will always try to resize its view so that it takes up the entire bounds of its superview. So if you want the tab bar controller to not do that, I would create another plain view controller that becomes your new root view controller.
In that view controller's view, you create 2 container subviews, one for your statsView and one for the tab bar controller. Now you add the tab bar controller's view to the one container view and the StatsViewController's view to the other.
(Note: I did not test this.)

Objective C: Using code to add a toolbar to a UITableView (within a Navigation Controller)

I managed to add a toolbar at the bottom of my UITableView using the code below:
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
toolbar.frame = CGRectMake(0, 436, 320, 50);
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
[self.navigationController.view addSubview:toolbar];
However when I try to switch back to the first page of the navigation controller, the toolbar at the bottom of the page is still displayed. How can I ensure that the toolbar is only shown on the UITable View and not any other views in the navigation controller?
Thanks in advance.
Zhen
In your TableViewController implement:
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.toolbar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
self.navigationController.toolbar.hidden = YES;
}