Custom Navigation Bar in ios6 and ios7 - ios

I have a problem to which I can't found a solution right now. In my application I have multiple buttons in my NavigationBar which are required throughout the app, rather than creating the buttons in every view controller I want to make a sub class of UINavigationBar or UINavigationController(i don't know which one). So that whenever the user moves between the views the navigation bar always contains those buttons. I have searched very much till now regarding this but couldn't found anything worth.
Please suggest me a way to do so, thanks in advance.

#import "CustomNavBar.h"
#implementation CustomNavBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.tintColor=[UIColor greenColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"Custom-Nav-Bar-BG.png"];
[image drawInRect:CGRectMake(0, 0, 40, self.frame.size.height)];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn drawRect:CGRectMake(42, 0, 40, self.frame.size.height )];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn2 drawRect:CGRectMake(82, 0, 40, self.frame.size.height )];
}
#end

You can subClass standard UINavigationBar to achieve this
#interface CustomNavigationBar : UINavigationBar
- (id)initWithFrame:(CGRect)frame;
#end
#implementation CustomNavigationBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn addTarget:self action:#selector(<#selector#>) forControlEvents:<#(UIControlEvents)#>]
[self addSubview:btn];
...
...
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] setFill];
UIRectFill(rect);
}
#end
To use this is StoryBoard or xib, simply change standard class name to CustomNavigationBar.
OR
If you want it programmatically
In AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navVC = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
}

Related

Issues in UITabBar with SideMenu

I created tab bar controller programmatically in objective c and I need to implement the side bar menu.Is it possible to implement the side bar menu in tab bar controller?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.tabBarController =[[UITabBarController alloc]init];
//Initialize View controller and speciality
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
self.tabBarController.viewControllers=[NSArray arrayWithObjects:viewcontroller1,viewcontroller2,viewcontroller3,viewcontroller4,viewcontroller5, nil];
self.window.rootViewController =self.tabBarController;
self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Download the library https://github.com/John-Lluch/SWRevealViewController
and copy the files SWRevealViewController.h & SWRevealViewController.m to your project
in AppDelegate.h
#import "SWRevealViewController.h"
#property (strong,nonatomic) SWRevealViewController *revealViewController;
in AppDelegate.m
self.revealViewController = [[SWRevealViewController alloc]initWithRearViewController:leftViewController frontViewController:tabBarController];
if ( self.revealViewController )
{
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(8, 65, 80, 30)];
[button1 setTitle:#"Side bar" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button1 addTarget:self.revealViewController action:#selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
[tabBarController.view addSubview:button1];//if u hav navigation controller u can replace button1 with BarButtonItem
[tabBarController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

Main.storyboard does not load

I'm new to iOS and I'm making some exercises I'm stuck at the begining. I'd like to use standard Main.storyBoard which looks like this:
And the AppDelegate.m file looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// CGRect bounds = [[UIScreen mainScreen] bounds];
// self.window = [[UIWindow alloc] initWithFrame:bounds];
//
// self.controler = [[ViewController alloc] init];
//
// self.window.rootViewController = self.controler;
//
// [self.window makeKeyAndVisible];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc = [storyboard instantiateInitialViewController];
// Set root view controller and make windows visible
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
And ViewControler if it will be usefull:
#interface ViewController ()
#property UIView *viewM;
#end
#implementation ViewController
-(void) loadView
{
CGRect bounds = [[UIScreen mainScreen] bounds];
self.viewM = [[UIView alloc] initWithFrame: bounds];
self.view = self.viewM;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"AHAHH DOTKNIETO EKRANU");
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.view.backgroundColor = [UIColor blueColor];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
//SET TEXT FOR THIS LABEL
label.text = #"TEXTTTTTT";
[self.view addSubview:label];
// Do any additional setup after loading the view, typically from a nib.
}
Everytime I run this sample it loads me View which is white instead of the image above. What's not right with it? I'm using Xcode 5.1.1 version.
In the project - General page you should see a setting for "Main Interface".
Remove all the code in the applicationDidFinishLaunching except return YES; and make sure this is set to Main.

UINavigationController: viewDidLoad is called but doesn't affect the UI

I have this in the App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
OneViewController *vc = [[OneViewController alloc] initWithNibName:#"OneViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
and this in OneViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[button setTitle:#"Hi" forState:UIControlStateNormal];
[self.view addSubview:button];
}
But when I run the app in the simulator, the OneViewController screen is blank. It has a xib file which is just the default blank xib.
the button's background color and the title's font color both are white by default in iOS 7. can you set the button's background color or button's title color to black to make the button to appear?

Photoscroller inside UINavigationController

I am trying to use this code inside an UINavigationController :
http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html
In storyboard :
I embed PageViewController in Navigation Controller.
I change Topbar to "NavigationBar".
I add a title : "ImageView" to NavigationBar.
I add a storyboard id to PageViewController and NavigationController.
In ImageViewScrollView.m :
- (void)displayTiledImageNamed:(NSString *)imageName size:(CGSize)imageSize
I change this line :
_zoomView = [[UIImageView alloc] initWithFrame:(CGRect){CGPointZero, imageSize }];
By these :
CGPoint navPoint = CGPointMake(0, 45);
_zoomView = [[UIImageView alloc] initWithFrame:(CGRect){navPoint, imageSize}];
And I change these lines too :
- (CGPoint)minimumContentOffset
{
return CGPointZero;
}
By these :
- (CGPoint)minimumContentOffset
{
CGPoint navPoint = CGPointMake(0, 45);
return navPoint;
}
And I don't see navigationBar when I use iOS Simulator. Replacing CGPointZero is not enough.
What's wrong ?
I found an easier way to put photoscroller inside UINavigationController.
I just added in appdelegate.h :
#property (nonatomic, retain) UINavigationController *navController;
and in appdelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// kick things off by making the first page
PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
if (pageZero != nil)
{
// assign the first page to the pageViewController (our rootViewController)
UIPageViewController *pageViewController = (UIPageViewController *)self.window.rootViewController;
pageViewController.dataSource = self;
[pageViewController setViewControllers:#[pageZero]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
self.navController = [[UINavigationController alloc] initWithRootViewController:pageViewController];
[[self window] setRootViewController:_navController];
}
return YES;
}

IOS UINavigationController, pushViewController not working

Good evening,
I currently have two UIViewControllers. My appDelegate looks like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
struct CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.x = rect.origin.y = 0.0f;
_viewController = [[sandboxViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
_window = [[UIWindow alloc] initWithFrame:rect];
[_window makeKeyAndVisible];
[_window addSubview:nc.view];
return YES;
}
The viewController looks like this:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = #"Master View";
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
}
- (void)switchView:(id)obj {
if(![self navigationController])
NSLog(#"navigationController IS NIL!!!");
if(!_secondView)
_secondView = [[secondViewController alloc] init];
[self.navigationController pushViewController:_secondView animated:YES];
}
By clicking on the info button, that was added to the right side on the navigation bar, I want to switch to the secondView. This, however, is not happening because navigationController logs as nil ! What am I missing here?
Any help is truly appreciated!
You don't have to create the window, it should already exist.
//_window = [[UIWindow alloc] initWithFrame:rect]; //remove this line
[self.window makeKeyAndVisible]; //use the ivar
[self.window addSubview:nc.view];

Resources