Objective C / UIBackBarButtonItem Why can't change view - objective-c

I've ran into an interesting and strange question while messing around with a project.
After spending like 3 hours doing it, I found out you can't change the view of the UIBackBarButtonItem, only the UILeftBarButtonItem, so if I want to implement a custom back button, I hide the UIBackButtonItem and display a UILeftBarButtonItem which does the popping.
Now I find it odd, that you can't change the UIBackBarButtonItem's view, but you can change the UILeftBarButtonItem and the UIRightBarButtonItem's views.
Can someone explain me why would Apple do this?

Actually, you can. Use UIBarButtonItem's instance method setBackButtonBackgroundImage:forState:barMetrics:.
So if you want to change the background image for all your back buttons, your code should like something like:
UIImage *backButtonBgImageNormal = [[UIImage imageNamed:#"back_button_bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 15, 5, 5);];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBgImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Use delegate method of UINavigationController, such like
#pragma mark -
#pragma mark - UINavigationController Delegate Methods
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//// customize you own stuff here;
UINavigationBar *morenavbar = navigationController.navigationBar;
UINavigationItem *morenavitem = morenavbar.topItem;
morenavitem.rightBarButtonItem.tintColor = [UIColor blackColor];
}

I think I have a comprehensive solution for you.
1) From experience, it's just best to not bow to limitations of BarButtonItems. I suggest you create a simple UIButton, customise it to your liking. maybe add an action to its touch events..brand it with custom background and title colors...etc.. keep a reference to this button, maybe as a property.
2) Then, you create an instance of UIBarButtonItem using the UIBarButtonItem initializer -initWithCustomView, you sneak in the UIButton instance as this custom view in the init and have complete control over it.
3) Finally, you just do this.
self.navigationItem.LeftBarButtonItems = #[ourUIBarButtonItem].
The navigation bar has an Array property "leftBarButtonItems" for series of left buttons, and rightBarbuttonItems for the right side. Just replace this with your own array, containing the UIbarButtonItem, that containing your button, and you having a reference to your button.
And now you can completely control you button that is properly in the navigation bar.
NOTE! - Once you provide such a leftBarButtonItem, the stock Apple BackButton is gone.

Related

dynamically generated ui bar button needs to push to popover using segue

zThis is based on a question I asked here which I have made good progressed already:
custom uitableviewcell will not display label texts
I basically followed the tutorial that was provided for me.
http://brianflove.com/2012/12/10/how-to-create-an-ipad-popover-view/
QUESTION
Now my issue is, I have to dynamically generate my ui bar buttons as if I just drag and drop it on the storyboard I am limited to one on the left and one on the right. This is my code to generate my button
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStyleBordered target:self action:#selector(getMenu:)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects:btn, nil];
}
Based on that tutorial I need to use a segue to popover the uitableview that I need. But that will require me to have an anchor point like so
So my question is, how do I add an anchor point to that UIBarButtonItem? I've been searching and I keep finding something regarding creating a custom popover class? Is this accurate?
I solved this issue by creating an invisible UIView on the Navigation Bar itself and then use that as the anchor point for the segue. The issue with that is the "arrow" of the popover when the uiviewcontroller gets displayed is awkwardly placed. Still trying to figure that out.
This is where I got the answer from
https://stackoverflow.com/a/14514837/639713

presentModalViewController gives a black screen

In an alert view method I implemented the following (pretty standard) piece of code for popping a modal view:
else if (buttonIndex == 1) {
EmergencyPlanViewController *emergencyPlanView = [[[EmergencyPlanViewController alloc] init] autorelease];
[emergencyPlanView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:emergencyPlanView animated:YES];
}
Somehow it gives me a black screen as result. I can't find what is wrong here.
I created the window in my MainStoryBoard and customized the class of the viewcontroller in IB to EmergencyPlanViewController.
The viewDidLoad method of the emergencyPlanView is triggered but it looks like the view is not loaded. Anyone an idea what's wrong here?
EDIT:
To be clear, I am not using seperate xib-files in my project. I only use the storyboard
In the xib file, is your UIView set to the File Owner's view. That is probably the problem. Also if you just apply init, that will load the EmergencyPlanViewControllerinterface builder file with the same name:
EmergencyPlanViewController.xib
So make sure in that case that either:
The EmergencyPlanViewController nib is indeed: EmergencyPlanViewController.xib
or that you write instead of init: initWithNibName://whatever nib name here
I managed to fix the black screen issue when presenting a modal view controller by setting a background color to the view in Interface Builder. I noticed that by default the background color of the view is set to something like black/white (see picture attached), although it appears in white. I don't know exactly what does this mean or how it is responsible for showing a black screen, but setting a single color or texture fixed it. PS: I've faced this when migrating from iOS SDK 5 to 6.
ios6
Try this:-
EmergencyPlanViewController *emergencyPlanView=[[EmergencyPlanViewController alloc] initWithNibName:#"EmergencyPlanViewController" bundle:nil];
emergencyPlanView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:emergencyPlanView animated:YES];

Adding BarButtonItems to backButton in view controller

i have a view controller, which is standalone and has two left UIBarButtonItem, however when i push it, i want to have these two buttons + the back button
i tried
- (void)viewDidLoad
{
[super viewDidLoad];
// back
if (self.navigationController.navigationItem.backBarButtonItem) {
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:self.navigationController.navigationItem.backBarButtonItem, self.barButtonFilter, self.barButtonFilterContacts, nil];
} else {
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:self.barButtonFilter, self.barButtonFilterContacts, nil];
}
}
if there is a back button, than add, else replace
but i didnt work
I am not able to get your problem but according to your caption you want to replace you back button of UINavigationController with a bar button item, in that case you simply need to have a custom button in place of back Button:
UIBarButtonItem *backButton= [[UIBarButtonItem alloc] initWithTitle:#"yourTitle" style:UIBarButtonItemStylePlain target:self action:#selector(someFunction:)];
self.navigationItem.rightBarButtonItem = backButton;
[backButtonrelease];
If this is not your problem please elaborate.
So the issue here is that a UINavigationBar can only have one leftButtonItem and one rightButtonItem. But What you can do is in the center of the UINavigationBar you can have a UIView. You can use this to place the buttons on.
Someone has the code here: adding-buttons-to-the-titleview-of-navigationbar-without-having-to-repeat-code
From Apple's iOS Human Interface Guidelines:
Use a toolbar instead of a navigation bar if you need to offer a
larger set of controls, or you do not need to enable navigation.
Avoid crowding a navigation bar with additional controls, even if
there appears to be enough space. The navigation bar should contain no
more than a view’s current title, the back button, and one control
that manages the view’s contents. If, instead, you use a segmented
control in the navigation bar, the bar should not display a title and
it should not contain any controls other than the segmented control.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html

Trying to dismiss subview and UIView

I'm still very new to iOS developing. In fact, if there is a super noob, I would be one :p. Currently I am working on creating an IBAction button that accesses a subview. I have 2 ViewControllers, AddClientVC and NewClientVC, both with .nib files. So basically, inside my AddClientVC I implement an IBAction button with the following code:
- (IBAction)buttonPressed:(id)sender
{
UIView *transparentBG = [[UIView alloc] initWithFrame:CGRectMake(-5, -5, 1500, 2500)];
transparentBG.backgroundColor = [UIColor blackColor];
transparentBG.opaque = NO;
transparentBG.alpha = 0.5;
[self.view addSubview:transparentBG];
transparentBG.center = transparentBG.center;
vc = [[NewClientVC alloc] initWithNibName:#"NewClientVC" bundle:nil];
[self.view addSubview:vc.view];
vc.view.center = self.view.center;
}
As you can see I implemented a UIView as a transparent background. Basically AddClientVC --> Transparent Background --> NewClientVC. Now I have created another IBAction button but this time inside NewClientVC as a function to dismiss the accessed subview which looks like this:
- (IBAction)saveDismiss:(id)sender
{
[self.view removeFromSuperview];
}
The problem I'm having right now is when I click the saveDismiss button it only removes the subview that I called previously on AddClientVC but it didn't remove the transparent background I have created as a UIView. So the problem is how do I implement an action which simultaneously removes my subview and the UIView transparent background I created.
I need all the help I can get :)
I'm not too sure I fully understand what you want to happen, but maybe you could try something like this?
- (IBAction)saveDismiss:(id)sender
{
[vc removeFromSuperView];
[self.view removeFromSuperview];
}
I recommend not to manage your screens by adding subviews manually but instead use
- (void)presentModalViewController: (UIViewController *)modalViewController
animated: (BOOL)animated
method on your root viewController.
Or better instantiate a UINavigationController and use push and pop methods to drill down/up your views.
See apple reference here
Do not worry about code execution speed and stay confident in apple's SDK. UIKit is optimized for best user experience. Trying to boost your code by doing inappropriate SDK use is, in my opinion, a risky strategy. ;) – Vincent Zgueb
Sorry Vincent but I don't agree with you. I reached here because I want to implement an gesture that adds a sub-view for my view, which will be the navigation of my app.
[self.view addSubview:ctrl.view];
is faster presenting the view than
[self.navigationController presentModalViewController:ctrl animated:NO]
and by the way, the solution to the topic in my case was:
[self.view sendSubviewToBack:ctrl.view];

Animating UINavigationBar

I want to slide the right button and the title in and out (instead of having things just appear and disappear). How can I go about doing this?
Instead of using property setter syntax, i.e. self.navigationController.navigationBarHidden = NO, use the method setter to specify an animated parameter:
[self.navigationController setNavigationBarHidden:NO animated:YES];
This should achieve what you want.
You can (sort of) animate the title by using a custom view for UINavigationItem's titleView property and doing the animation yourself. As for the right button, you can't animate it without completely rewriting UINavigationBar (not recommended).