Add image UIBarItem to UINavigationController or alternative - ios

I have a UINavigationController with a left and a right button on an app being used as a remote control for a piece of hardware. I would like to show that the hardware is connected to the app by displaying an icon in the navigation bar to the left of the right button. From the documentation it looks as though I can only add UIBarButtonItems which make me suspect there is another, more conventional place for my 'connected LED' indicator.
So my question is can I display an icon in a UINavigationController navigation bar and if not where should I display it?

UINavigationBar is a subclass of UIView, so you can add items to it like this:
[navBar addSubview:whatever];
The navigation bar is a property of the navigation controller (i.e. you can reference it like this self.navigationController.navigationBar).

There isn't really a "conventional place" for something like this. :)
I suspect this 'connected LED' should be displayed on all views, regardless of the current view (and its UINavigationItem). If that is correct, the easiest way would probably be to NOT put that icon in the actual UINavigationBar, but place it as a separate UIView in the UINavigationBar's superview.

you should be able to just create a uiview programatically and add it as a subview of the navbar
UIImageView *connectedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"connected-icon.png"]];
[self.navigationController.navigationBar insertSubview:connectedView atIndex:0];
[connectedView release];
if insertSubview doesn't work as you expect try addSubview:
[self.navigationController.navigationBar addSubview:connectedView];
You probably want to create the connectedView as a property though so you can (more) easily remove it when you are no longer "connected".
see this other examples of the approach

try this code
[[[yourViewController viewControllers] lastObject] navigationItem].titleView = yourImageView;
worke for me in customising navigation bar in mail controller. Hope you get some idea from here.

Related

iOS view doesn't display any of it's subviews when added as a subview of NavigationController's view

I have a NavigationController and I want to make a View that animates itself from the top of the screen and sits above navigationBar. I'm initialising viewcontroller from a xib and it works perfectly when I add it as [self.view addSubview:myView]; , but it sits below navigation bar. When I try to add it as [self.navigationController.view addSubview:myView]; it places it on top of navigationBar as intended but view doesn't display any of it's subviews placed in xib. I can't see why this is happening, have been trying to solve this for hours.
UPDATE
My custom view from a xib is paired with custom ViewController class and I added this code in it :
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"!!! %i", [self.view.subviews count]);
}
if I add myView to self.view it prints 1 (there's actually 1 subview), but when I add it as self.navigationController.view -viewDidAppear is not even called. But it does appear on the screen with no subviews. Now I'm even more confused.
I think you need use this:
[self.navigationController.navigationBar addSubview:myView];
Why don't you just remove the navigation controller if you don't need it? Another way is to hide the navigation bar so that view occupies complete space and then show the navigation bar again when needed.
I could help more if you provide some code with the views.
Turns out I'm an idiot. It's a really simple scope problem. I was instantiating a local variable of my VC inside the method, and of course it was released at the end. As soon as I assigned my VC to a global variable all started to work like a charm. Easy as that.

UITableViewController UI issues in swift

I'm using Parse to set up a log in system for my app. When the user logs in, they're taken to a TableViewController with different options to choose from. For some reason, the TableView comes all the way up through the status bar, and it doesn't look good. Also, in my code is set the TableView's title, but that title doesn't show up. Here are the properties of the TableViewController.
And here's the status bar, where the title should be.
How do I fix the problem?
It sounds like you would like to have a UINavigationBar atop your view. That's easy to do!
Wherever you are present/displaying your TableViewController (maybe it looks like this now)
MYTableViewController *vc = [[MYTableViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:vc];
instead do the following: (Making up a class name here)
MYTableViewController *vc = [[MYTableViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:navController];
OR if you are using Storyboards to present your VC (it looks like you are using a storyboard for some parts at least)
Just drag on a Navigation Controller object and reroute the ViewController it brings out with
Oh yeah and the 'Title' property is for UITabBarController containers mostly. When you get this set up with a navigation controller, you will be able to set the title of that by setting
self.navigationItem.title = #"Test"
or in the Nib by selecting the newly appeared navigation bar and changing the title property; you'll see it if you went with the nib approach.
I think you should set the tableview some constraints.
To make this clear, I make a small project containing just one viewController, which has a tableview as its subview. After that, I attach the tableView some constraints By the follow steps:
1.select the tableview in the storyboard;
2.click Editor->Pin in the menu;
3.select "leading space to superview","trail space to superview","top space to superview","bottom space to superview"
4.open the utility area and select "Show the Size inspector". Edit the constraints to make sure the value of "top space to superview" equals to 20(because 20points is the heigh of the status bar), while the the other three constraints' value equal to 0.
After doing these, you will create a tableview which lies 20point under the status bar.
I think posting some images would be much better, but I'm new to stackflow, so I haven't gain enough reputation to post a image, sorry for that.

UIBarButtonItem not disabled until view changes

I have a UINavigationController and am using the toolbar in one of my view controllers. I have several UIBarButtonItems. At various points, I disable certain buttons in the toolbar, using things like _btnEdit.enabled = NO.
This all works well except for one time where this happens when there is no user interaction. In that case, the button appears to be enabled (isn't grayed out), but doesn't accept touches. If I cover the bar with something (an action sheet from the bottom) or change the orientation of the device, it shows correctly.
I've tried self.navigationController.toolbar setNeedsDisplay] and [self.navigationController.toolbar drawRect:self.navigationController.toolbar.bounds] but neither have an any effect.
Any ideas on how to "refresh" this view? I know UIBarButtonItems don't inherit from UIView, which I feel like may be contributing to the issue.
This is the intended behaviour. The setNeedsDisplay is a good reflex try, but you don't own the navigation bar and UIBarButtonItem doesn't inherit from UIView, so we need to think of them a little differently.
Here how you can achieve your goal :
UIBarButtonItem *bbi = self.navigationItem.rightBarButtonItem;
bbi.enabled = NO;
[self.navigationItem setRightBarButtonItem:bbi animated:YES];
NOTE : self is a UIViewController
I've just made a quick test with this and it's working.

What's the difference between UINavigationController and UINavigationItem

I don't understand what is the difference between
self.navigationcontroller.navigationitem and self.navigationitem
i have navigation based application and in viewDidLoad method in rootViewController of navigationController I set the NavigationItem titleView to custom image by using this code
UIImageView* titleImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
self.navigationItem.titleView =titleImage;
by the way if i try to change titleview by this way
UIImageView* titleImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
self.navigationcontroller.navigationItem.titleView =titleImage;
nothing was happened.
, and when navigationcontroller pushed the another view to the stack the titleview of navigationitem is cleared and reference sets to nil, and also i noticed that the references of self.navigationcontroller.navigationitem and self.navigationitem are not the same.
I believe what you are asking is the following: self.navigationController.navigationItem and self.navigationItem. I am ignoring the rest of the question since it is not really essential to the question.
From what I understand self.navigationController.navigationItem is useless since it is accessing navigation item of the navigation controller (ie your rootViewController).What you really want is self.navigationItem, navigation item of the view controller because that is what essentially going to show up in your view controller.
UINavigationController is a subclass of UIViewController so self.navigationController.navigationItem is just a spillover method from subclassing. It does not do anything positive (at least in my experience).
Edit: Read this for further clarification.
They are Different. If you dynamically add UINavigationController, then you use self.navigationController.navigationItem.
Please better clarify your question.

Hide the top bar in UITabBarController

How do I hide the top bar in UITabBarController using OBJ-C code (without using Interface Builder) ?
Please see the image in the link below to better understand my requirement.
http://skitch.com/jugsvn/dxkr7/iphone-simulator
I already have another toolbar as you can see and I don't want this. I'm not using XIB / NIB files and creating all the objects using plain OBJ-C code.
Cheers
Jugs
The topBar is part of the UINavigationController not the UITabBarController, you can hide it by putting the following in your viewWillAppear method:
self.navigationController.navigationBarHidden = YES;
But I'm pretty sure the navigationBar is always on top, so I think the bar you want to hide is actually your own toolbar.
self.myToolBar.hidden = YES;
self.tabBarController?.navigationController?.navigationBar.hidden = true

Resources