UINavigationController barstyle property changes layout - ios

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;

Related

UINavigationItem's title disappears when view controller is pushed

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

UISegmentedControl within UIView does not respond to change event

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

UINavigation title not showing?

This is in my view did load. There is no title showing at all? I have a UITabBar with a UITableView and UInavigationBar inside the UITabBar.
//Creates navigation bar.
UINavigationBar *myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
myNavBar.barStyle = UIBarStyleBlack;
myNavBar.topItem.title = #"My Title";
[self.view addSubview:myNavBar];
myNavBar.topItem.title = #"My Title";
doesn't work because topItem is nil since there is no item. You need to add UINavigationItems.
self.title = #"Some Title";
doesn't work because UINavigationController picks up UIViewController's title and there is no UINavigationController here.
self.navigationItem.title=#"My Title"";
doesn't work because navigationItem is nil.
Solution:
Add an item (and set the title in the UINavigationItem) using either -[UINavigationBar pushNavigationItem:animated:] or -[UINavigationBar setItems:]
Use this
self.navigationItem.title=#"My Title"";
It may help you.

UINavigationBarItem programmatically

I have view controller which is inherited from UINavigationController.
On viewDidLoad I want to set rightBarButtonItem.
I do following.
UINavigationItem* navItem = self.navigationItem;
UIButton* btnOptionsView = [[UIButton alloc] init];
[btnOptionsView setImage:[UIImage imageNamed:#"options.png"]
forState:UIControlStateNormal];
[btnOptionsView sizeToFit];
UIBarButtonItem* btnOptions = [[UIBarButtonItem alloc] initWithCustomView:btnOptionsView];
navItem.rightBarButtonItem = btnOptions;
but the button is invisible, in debug mode I have noticed that btnOptions.width is 0, and also I can't set the title for navItem, something like this
[navItem setTitle:#"title"]
does not change the title of UINavigationItem.
You should not subclassing UINavigationController. From apple documentation:
The UINavigationController class implements a specialized view
controller that manages the navigation of hierarchical content. This
class is not intended for subclassing. Instead, you use instances of
it as-is in situations where you want your application’s user
interface to reflect the hierarchical nature of your content.
try to change the title of the button

UINavigationBar - Set title programmatically?

I have a tab bar application with a different view on each tab. Each view has a UINavigationBar with title set on Interface Builder. I want to change the title based on a clause in the ViewDidLoad method, so if x { change the title }.
I have tried self.title = #"title", but this changes the title of the tab bar item itself.
So, how is this done?
I've set the title programmatically using code something like this:
navBar.topItem.title = #"title";
where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.
If navBar.topItem is the tab bar item, I don't see a way for you to change the title that appears on the navigation bar without also changing the title on the tab bar item, since the navBar's topItem and the tab bar item is the same object.
Use
self.navigationItem.title = #"the title";
as setting navBar.topItem.title will not work in all circumstances.
Use this :
CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];
UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = #"Your Title";
[navBar pushNavigationItem:navItem animated:false];
[self.view addSubView:navBar];
or
self.tableView.tableHeaderView = navBar; etc
If the class is a type of UIViewController, then you can set title as given in the viewDidLoad method.
[self setTitle:#"My Title"];
Create IBOutlet of UINavigationBar
navigationBar.topItem.title = #"Title";
Hope this helps.
In ViewDidLoad of your ViewController.m just write,
self.navigationItem.title=#"Hello World";
Here is the Output:
I used the following:
self.navigationController.navigationBar.topItem.title = #"Title";
However, I also had to call it in a dispatch block as it wouldn't change the title when called within the viewDidLoad w/o it.
Note that in order for the title to show up AT ALL the Navigation Controller's delegate MUST be set to the navigation bar itself or the title will NEVER show!
I achieved this way in Swift 5
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Title"
}
You can also just set the title in the ViewDidLoad under your ViewController or TableViewController using
_title = #"Title";
or
self.title = #"Title";
Try this,
self.navigationItem.title = #"Your title";

Resources