Different backgroundimages for UINavigationBar - ios

Is it possible to use per-view backgroundImage in the UINavigationBar?
In the view loaded into the stack of UINavigationController, I tried :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
[[self navigationController] setNavigationBarHidden:NO];
UINavigationItem* a = [self navigationItem];
[a setTitle:#"MY TITLE"];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"mybar.png"] forBarMetrics:UIBarMetricsDefault];
}
But the image is not shown at all.
Is it possible to change the navigation bar image for every uiview loaded into the stack as it is loaded? In other words, different background image for different uiview loaded in the navigationcontroller.

Related

iOS Layout custom view create with xib when resized

I created MyCustomView which has two labels. One at left and one with right align and at right. It has about 30px height (it's not important). I connect it to xib and it works. In .xib I set main view width to 400px and set autolayout constraints for labels.
Now the problem. When I added to my controller in IB with UIView and set class to MyCustomView I see left label but right label is out of screen. I set constraints right but It doesn't work. When I tried to resize MyCustomView in .xib by mouse and moving with edges it's okay but when I set width value "manually" in right column it doesn't layout right (it doesn't change at all). Where is the problem? How can I fix this?
#implementation MyCustomView
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
self.backgroundColor = [UIColor clearColor];
self.view = [[[NSBundle mainBundle] loadNibNamed:#"MyCustomView"
owner:self
options:nil] firstObject];
[self addSubview:self.view];
//self.translatesAutoresizingMaskIntoConstraints = NO;
}
- (void)awakeFromNib {
[super awakeFromNib];
//[self setNeedsUpdateConstraints];
//[self setNeedsLayout];
}
#end
As you can see in comments I tried some ways but nothing helped.
New File --> iOS (Source) -- Cocoa Touch --> Next
Enter Name
Add code in MyCustomView.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//self.view.backgroundColor = [UIColor redColor];
self.preferredContentSize = CGSizeMake(30.0, 400.0);
}
return self;
}

iOS: Add UIView below UINavigationBar

if I try to add a view to the navigation bar in my custom UINavigationController with this code:
- (void)viewDidLoad {
[super viewDidLoad];
self.sv = (MyCustomView *)[[[NSBundle mainBundle]loadNibNamed:#"MyCustomView" owner:self options:nil] objectAtIndex:0];
self.sv.frame = CGRectMake(self.navigationBar.frame.origin.x, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, heightOfMyCustomView);
[self.navigationBar addSubview:self.sv];
}
and
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated];
viewController.view.frame = CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y + heightOfMyCustomView, viewController.view.frame.size.width, viewController.view.frame.size.height - heightOfMyCustomView);
}
when I push a controller, the top part of the controller's view is hidden by my custom view I added to the navigation bar.
How can I put the controller's view down below my custom view?
I'd also suggest to take a look at UINavigationItem.titleView property, that gives you an ability to provide your view for navigation bar title area.
In your custom UINavigationController init method assign delegate to itself, like this:
CustomeNavCtrl.m file:
#implementation CustomeNavCtrl
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.delegate = self;
}
return self;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//Todo:viewController.view layout
//Todo:send MyCustomView to back or something other you like.
}
CustomeNavCtrl.h file:
#interface CustomeNavCtrl : UINavigationController<UINavigationControllerDelegate> {
}

Tab bar element onclick IOS

I have a master utility application embedded in a tab bar. Now, for this tab bar, i have the following.
#interface MainTabViewController : UITabBarController< UITabBarControllerDelegate>{
}
#end
#implementation MainTabViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(#"One");
if(self.tabBarController.selectedIndex==1)
{
NSLog(#"clicked");
}
if (self.tabBarController.selectedIndex==2) {
NSLog(#"Helo");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
The code is not reaching the didSelectViewController at all. I am new to IOS. Could anyone plz guide where i am going wrong??
Add delegate method to the UITabBarController
either using storyboard (if you are using)
or as it is UITabBarController do using
self.delegate = self ;

Adjust iOS TabBar item title to the centre of it

//TabBarController code:
self.delegate=self;
self.tabBarController.tabBar.delegate=self;
CGRect viewFrame=self.tabBar.frame;
viewFrame.origin.y -=0;![enter image description here][1]
viewFrame.origin.x -=0;
viewFrame.size.height=30;
self.tabBar.frame=viewFrame;
firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL];
secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];
NSArray *twoViewControllers = [[NSArray alloc] initWithObjects:
self.firstViewController, self.secondViewController, nil];
self.viewControllers=twoViewControllers;
// ====================================================
//
// FirstViewController code in initWithNibName:
//
// To set the title of the first tabbar item:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Article view";
NSLog(#"count = %d",[self.tabBarController.tabBar.items count]);
}
return self;
}
//How can i make the first tabbar item title "Article View" to the Center without adding any //image to the tabbaritem ?
// similar to the below tabbar items screenshot.
[1]: http://i.stack.imgur.com/xBpVH.png
Thanks in advance.
Replace the initWithNibName method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Article view";
self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
NSLog(#"count = %d",[self.tabBarController.tabBar.items count]);
}
return self;
}
self.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0); this line here adjusts the position of the image for the tabBarItem in the manner :
Shift the image in x-direction '+5' and in y-direction '-5' from the
default position.
Play with UIEdgeInsetsMake and have fun. Cheers.

Adding a Navigation Controller but navigation item not displaying

Originally I had a custom NavigationBar, without a navigation controller. I have a sliding menu.
On the home screen there is a tableview, with a list. I would like to be able to add items to this list. So, I had to add in a navigation controller after the fact. I added the navigation controller to the storyboard and linked it to my main view controller [the one with the list]. Then I dragged in a view controller. I dropped in a bar button item into the navigation item, and pushed it to the recently added view controller.
I ran my app and the app shows no navigation bar or navigation item whatsoever now. Originally I had a custom navigation bar.
I'm not too sure what's going on here. Thanks for any help.
- (void)customizeAppearance {
UIImage *NavBG = [UIImage imageNamed:#"nav bar.png"];
[[UINavigationBar appearance] setBackgroundImage:NavBG forBarMetrics:UIBarMetricsDefault];
init View Controller code from .m
#import "initViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#interface initViewController ()
#end
#implementation initViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
}
from .h
#import "ECSlidingViewController.h"
#interface initViewController : ECSlidingViewController
#end
from main.m
#import "MainViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
{
NSArray *toDoList;
NSIndexPath *selectedIndexPath;
}
#synthesize menuBtn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
from main.h
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
}
#property (strong, nonatomic) UIButton *menuBtn; // go to MenuViewController.m and synthesize
#end
Second view controller.m file
#import "SecondViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize menuBtn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Below is the same stuff pasted from MainViewController.m
// Do any additional setup after loading the view.
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowOpacity = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
menuBtn.frame = CGRectMake(8, 10, 34, 24);
[menuBtn setBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal];
[menuBtn addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.menuBtn];
}
Here are all the arrays I use
From the image you are showing, it doesn't look like any of the VCs have the starting arrow to its left. If that is the case, go to SB, click on the Nav controller, go to Attributes inspector on the right,scroll down and check the "Is initial View Controller".
Hope this answers it.
Update: Guessing at your code, you are trying to modify an immutable NSArray. Change your declaration to NSMutableArray *toDoList.

Resources