I am having an issue with my view controller's navigation item's title. Indeed, I am setting it in the viewDidLoad method like this :
self.navigationItem.title = "Title"
The problem is that when I push this view controller from another on to the navigation stack, I see the title while the transition but as soon as the transition ends, it disappears... I checked in the debugger, the title is still set... I tried with the titleView property either with no success...
By the way, the problem only show up on iPhone 5 with iOs < 9...
Let's try that as an answer:
CGSize result = [[UIScreen mainScreen] bounds].size;
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, result.size.width, 40)];
_navItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
[_navBar setItems:#[_navItem]];
[self.view addSubview:_navBar];
Related
I have programmatically created a basic app on iOS7 SDK :
Created two UINavigationController
Added a view to each of them
Created a UITabBarController with two items
Added each UINavigationController to its respective UITabBarController
This is working fine, and everything is done programmatically. My project only consists in a simple window app template, without any xib nor storyboard, and everything is done within application didFinishLaunchingWithOptions: of my AppDelegate.m.
I am trying to add a UISegmentedControl (still programmatically) as a subview of the view of the first UINavigationController, as follows :
// First navigation controller
UINavigationController *firstNavController = [[UINavigationController alloc] init];
firstNavController.navigationBar.barTintColor = [UIColor redColor];
firstNavController.title = #"First";
firstNavController.navigationBar.topItem.title = #"First top";
firstNavController.tabBarItem.image = [UIImage imageNamed:#"FirstTab"];
// First view
UIView *firstView = [[UIView alloc] init];
firstView.backgroundColor = [UIColor yellowColor];
// Add a segmented control to this view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"One", #"Two", nil]];
// Register an event handler
[segmentedControl addTarget:self action:#selector(segmentedControlSelectedIndexDidChange:)
forControlEvents:UIControlEventValueChanged];
// Shape it and select its first item
segmentedControl.frame = CGRectMake(10, 100, 300, 20);
segmentedControl.selectedSegmentIndex = 1;
// add the segmented control to the view
[firstView addSubview:segmentedControl];
// and the view to the first tab's navigation controler
[firstNavController.view addSubview:firstView];
The UISegmentedControl DOES show up, but touch events are not handled.
If I change my code and add the UISegmentedControl directly within the UINavigationController, touch events are actually responding :
// and the segmented control directly within the navigation controller
[firstNavController.view addSubview:segmentedControl];
What am I doing wrong, and what should I do to have it respond to it's touch events ?
You need to set the size of the view.
Confusingly a view will be visible even if it is too small to fit it's content but will only respond to touches over the area of it's offical size.
You should not be adding a subview directly to the navigation controllers view.
'firstView' should be encapsulated into a UIViewController. And create your navigation controller with [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
I have following code to create UINavigationBar and set the navigation item with a back button at the right side.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
navBar.delegate = self;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backButtonTapped)];
UINavigationItem *backItem = [[UINavigationItem alloc] init];
[backItem setTitle:#"What's New"];
[backItem setLeftBarButtonItem:back];
backItem.leftBarButtonItem.enabled = YES;
[navBar pushNavigationItem:backItem animated:NO];
[self.view addSubview:navBar];
This worked perfectly until i update my xCode 5 to iOS 7.1 update recently.
But now when the UIView presented the navigation button is not visible. But when i touch the location of the button (where it used to be before the update), it shows me the button and click even is firing.
My question is how to set the button visible at the moment view is present to user ?
Thank you.
Try to initialize your UIBarButtonItem with initWithCustomView. Also, try to set buttonType of the UIButton to UIButtonTypeSystem.
Thanks to all,
finally what happen is as follows.
The above behaviour is not what i assume it is. When a new view is loaded the button is disappear by default and only if i move my finger here & there on the location button used to be it appears. Further when i keep the view still, after it loaded, about 15 seconds, button appears.
So i suspect this is with "viewDidLoad" method. (Code related to this UINavigationBar is in viewDidLoad method.
When I move above code to "viewDidAppear" method it start to work again.
In my current project I have an app based on a navigation controller. Some of the view in the app require a toolbar at the bottom of the view. I am creating the toolbar programmatically with the following (excerpted from viewDidLoad in a table view controller):
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshList)];
[reloadButton setTitle:#"Refresh"];
[reloadButton setTintColor:[UIColor whiteColor]];
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
438,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:reloadButton, nil];
[self setToolbarItems:toolbarItems];
[self.navigationController setToolbarHidden:NO];
This has worked well in most views. However, I am now incorporating it into another view using identical code. In that controller, the toolbar appears about an inch above the bottom of the view--it doesn't snap to the bottom of the view as it does in other view controllers. Once I have navigated to the problem view, the other toolbars in other view begin exhibiting the same behavior. This is only happening in the simulator, not on my physical device. However, I currently have only an iPhone 4 as my physical device. Is this a bug in the simulator or an indication of an issue somewhere? THanks!
You are setting the y position to a hardcoded value of 438. This will result in it not appearing at the bottom of the screen for the taller iPhone 5's). You should calculate the y position as well perhaps with:
CGRectGetMaxY(self.view.bounds) - toolbarHeight
this would result in:
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
It could also have to do with the presence of a navigation bar and the status bar
I've run into a weird situation. This is all done in code - no Interface Builder.
I'm creating a UIViewController and adding some content to it:
UIViewController* popoverViewController = [[[UIViewController alloc] init] autorelease];
UIView* popoverContentView = [[[UIView alloc] init] autorelease];
popoverContentView.backgroundColor = [UIColor blackColor];
// Add some stuff to popoverContentView
popoverViewController.view = popoverContentView;
I then create a UINavigationController, set its root view controller to the UIViewController from above, and add a title and a button to the navigationItem:
UINavigationController* popoverNav = [[[UINavigationController alloc] initWithRootViewController:popoverViewController] autorelease];
popoverViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissPopover)] autorelease];
[popoverViewController.navigationItem setTitle:#"MY TITLE"];
Then I set up a UIPopoverController with the UINavigationController in it and present it:
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:popoverNav] autorelease];
self.popoverController.delegate = self;
[self.popoverController setPopoverContentSize:CGSizeMake(320, 216) animated:NO];
[self.popoverController presentPopoverFromRect:self.cell.frame inView:self.popoverParentView permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown animated:YES];
The problem is that everything appears correctly with one exception: the navigation bar is invisible, but the title text and the bar button still show up and work correctly. I've tried messing with the bar's hidden and tintColor properties and changed the size of the popover, but nothing changes.
I'm sure I'm missing something obvious, but I can't see it.
I have other examples of similar things in my project's codebase, but those appear correctly. Any ideas as to why this is happening or how I could fix it?
EDIT
Not sure if the following will help, but I'm hoping it will provide some clues as to what's really going on here to someone who's seen something like this before.
I pushed a new (blank) view controller onto popoverNav just to see what would happen. It pushes and animates perfectly. Everything looks right except that the nav bar is still transparent and the bar button items are pushed to the top of the view.
Perhaps not the main cause of the problem, but it looks like the UIDatePicker is clipped by the CGSize you have setup. As far as I can remember, the default height for the picker is 216 (which you have) but you haven't taken into consideration the required height for the UINavigationBar.
I'm not sure why the background of the view looks to be some kind of textured grey unless this is set in your UIViewController but it doesn't appear that way from the code you pasted. This certainly isn't a default texture for iOS though.
Perhaps try and make your CGSize a little larger to accommodate the contents of the view as it looks as though the "Done" button is hugging the top right corner where there should be a fair few points worth of spacing around this by default.
Also, is there a reason why you're creating a new view and assigning it as the view property of your view controller?
UIView* popoverContentView = [[[UIView alloc] init] autorelease];
popoverContentView.backgroundColor = [UIColor blackColor];
// Add some stuff to popoverContentView
popoverViewController.view = popoverContentView;
Why not add the content as subviews of the view instead?
I have a modal view controller which displays a navigation controller. The navigation controller in-turn has a regular UIViewController as its root view controller. The only UI element that the above-mentioned UIViewController has is a UISwitch.
Now here's the problem: When I change the barStyle property of the navigation controller, the layout of the UISwitch inside the UIViewController changes. Here's what I am talking about:
If I don't set the barStyle property, here's what I get:
http://img535.imageshack.us/img535/2281/plaini.png
The UISwitch is now in its 'exepected' place.
Now if I set the barStyle property,
navController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Notice that the UISwitch is behind the navigation bar:
http://img853.imageshack.us/img853/2377/blackya.png
Here's the code for the UISwitch in the UIViewController:
- (void)viewDidLoad
{
UISwitch* mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[self.view addSubview:mySwitch];
[mySwitch release];
}
Can someone help me understand what's happening?
The Frame shifts when you use the UIBarStyleBlackTranslucent (which is actually deprecated) property because it assumes you want your view to be underneath
The Apple Documentation says to use the following since UIBarStyleBlackTranslucent is deprecated:
navController.navigationBar.barStyle = UIBarStyleBlack;
navController.navigationBar.translucent = YES;
You could try shifting your view back in the correct place or try using the following:
navController.navigationBar.tintColor = [UIColor blackColor];
navController.navigationBar.translucent = YES;