How to achieve fixed side navigation in iOS? - ios

I am new to iOS development. I am developing an app which will contain fixed side menu on left side of all ViewController. When user will select any menu out of 5, selected menu will highlighted in different colour, but its later part.
How do I achieve the fixed side menu?
I am stuck on this. I search so many sites on Google but nothing helpful came out of it. Can anyone provide sample code?
Any help would be appreciated.
Thanks & Regards.

Please try this code.
First create a UIView type class.
Write this code TabbarController.h Class
#import <UIKit/UIKit.h>
#class TabbarController;
#protocol Tabbar_protocol <NSObject>
#required
-(void)First_viewController;
-(void)Second_viewController;
-(void)Third_viewController;
-(void)Fourth_viewController;
-(void)Fifth_viewController;
#end
#interface TabbarController : UIView // this is your UIView type class
-(void)CreateTabbarController;
#property (nonatomic, assign) NSObject <Tabbar_protocol> *delegate;
#end
This code for TabbarController.m Class
#import "TabbarController.h"
#implementation TabbarController
#synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self CreateTabbarController];
}
return self;
}
-(void)CreateTabbarController
{
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0, 0, 50, 96)];
[button1 setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button1 setTag:101];
[button1 addTarget:self action:#selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setFrame:CGRectMake(0, 96, 50, 96)];
[button2 setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
[button2 setTag:102];
[button2 addTarget:self action:#selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setFrame:CGRectMake(0, 192, 50, 96)];
[button3 setBackgroundImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateNormal];
[button3 setTag:103];
[button3 addTarget:self action:#selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button3];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setFrame:CGRectMake(0, 288, 50, 96)];
[button4 setBackgroundImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateNormal];
[button4 setTag:104];
[button4 addTarget:self action:#selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button4];
UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
[button5 setFrame:CGRectMake(0, 384, 50, 96)];
[button5 setBackgroundImage:[UIImage imageNamed:#"image5.png"] forState:UIControlStateNormal];
[button5 setTag:105];
[button5 addTarget:self action:#selector(TabbarBtn_Action:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button5];
}
-(void)TabbarBtn_Action:(id)sender
{
switch ([sender tag])
{
case 101:
if (self.delegate != nil)
{
// code for changing your button color
[self.delegate First_viewController];
}
break;
case 102:
if (self.delegate != nil)
{
// code for changing your button color
[self.delegate Second_viewController];
}
break;
case 103:
if (self.delegate != nil)
{
// code for changing your button color
[self.delegate Third_viewController];
}
break;
case 104:
if (self.delegate != nil)
{
// code for changing your button color
[self.delegate Fourth_viewController];
}
break;
case 105:
if (self.delegate != nil)
{
// code for changing your button color
[self.delegate Fifth_viewController];
}
break;
default:
break;
}
}
#end
In your all ViewController.h import this class & ViewController.m class call that delegate method.
#import "TabbarController.h"
#interface ViewController : UIViewController <Tabbar_protocol>
{
}
// ViewController.m class
-(void)First_viewController
{
// Navigate to FirstViewController...
}
-(void)Second_viewController
{
// Navigate to SecondViewController...
}
-(void)Third_viewController
{
// Navigate to ThirdViewController...
}
-(void)Fourth_viewController
{
// Navigate to FourthViewController...
}
-(void)Fifth_viewController
{
// Navigate to FifthViewController...
}
Like this you have to call in all 5 ViewController.

Related

UIButton like Rating bar that i want to do anyone help me?

i want to use button is in either way
i want to use if user select one time button then its display different color after that click on button it appear as usual after click it display that color
static int tapCount = 0;
- (IBAction)BtnClickdemo:(id)sender
{
tapCount++;
if (tapCount == 1)
[btnClick setImage:[UIImage imageNamed:#"line.png"] forState:UIControlStateNormal];
else if (tapCount == 0)
[btnClick setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
else
{
[btnClick setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
}
}
User only select 2 tappded like Ratingbar taped ??
How can i do ??
You can assign a BOOL value to your button and check this value everytime the button is tapped:
#implementation youCustomViewController {
BOOL buttonSelectionned;
UIButton *myButton;
}
- (void)viewDidLoad
{
/* Init objects */
buttonSelectionned = NO;
[super viewDidLoad];
// Do any additional setup after loading the view.
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100);
myButton setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
and then implement your method buttonTapped
- (void)buttonTapped:(id)sender
{
if (buttonSelectionned) {
buttonSelectionned = NO;
myButton setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
} else {
buttonSelectionned = YES;
myButton setImage:[UIImage imageNamed:#"MinSelected2.png"] forState:UIControlStateNormal];
}
}

UIButton image does not show in Navigation Bar

I am trying to a slide menu. The only problem is that the image for the UIButton does not show at runtime. This is the code I have written.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSString *)segueIdentifierForIndexPathInLeftMenu:(NSIndexPath *)indexPath
{
NSString *identifier;
switch (indexPath.row) {
case 0:
identifier=#"firstSegue";
break;
case 1:
identifier=#"secondSegue";
break;
}
return identifier;
}
-(void)configureLeftMenuButton:(UIButton *)button
{
CGRect frame = button.frame;
frame.origin=(CGPoint){0.0};
frame.size = (CGSize){40.40};
button.frame = frame;
[button setImage:[UIImage imageNamed:#"icon-menu"] forState:UIControlStateNormal];
}
I suppose you are using navigation controller. If so, implementation could look like:
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureLeftMenuButton];
}
-(void)configureLeftMenuButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"icon-menu"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
[button addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlStateNormal];
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = menuItem;
}

Is there a way of doing an action locally on a UIButtton?

I am setting up a table with an I have this code:
UIButton *done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
[done setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[done addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:done];
return cell;
This doesn't work beacuse this button is only declared in this scope and I want to simply change its image on this click
Change the image in the done: method.
- (void)done:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setImage:[UIImage imageNamed:#"yourImage.png"]
forState:UIControlStateNormal];
}
What does you done method look like?
I think that if you add a sender argument to the method, you can access the button.
- (void)done:(id)sender
{
UIButton *button = (UIButton*)sender;
[button doStuff];
}
You have to declare an ivar or a private/public property. So you can change its image from everywhere, for example:
#interface ViewController ()
#property UIButton *done;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
[self.done setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[self.done addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.done];
}
- (void)done:(UIButton *)sender
{
//Do something.. like change image
[self.done setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
#end
Or you declare the sender as the UIButton, like this:
- (void)done:(id)sender
{
UIButton *done = (UIButton *)sender;
//Do something with the button
}

Toggle Button Image

I have multiple of buttons.
They all load in the viewDidLoad. I want it so once one button has been pressed, it changes to the other image.
So Img1 loads via the viewDidload, then once it's been pressed, it goes to Img2
This is my current code which i have tried:
In the viewdidload:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
In the ibaction (which i tried, but im guessing)
-(IBAction)ButtonAction
{
if (btn1==0)
{
btn1=1;
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
}
else
{
[btn1 setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
btn1=0;
}
}
Thanks for the help
You need to make sure you are hooking up your UIButton to the proper selector. Also, I would use an #property or a global / instance variable to track which image is currently in the UIButton. Here's some code that should do the trick:
//... in #interface
#property (nonatomic, strong) NSString *btn1ImageName;
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.btn1ImageName = #"img1.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"img1.png"]) ? #"img2.png" : #"img1.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
if (btn1==0)
I don't get that part.
One easy solution would be to have two different images for a selected and un-selected state.
btn1.selected = YES, will automatically choose the selected button image.
btn1.selected = NO, will automaticall choose the un-selected button image.

how to load same tableview with different NSArrays depending on uibuttons tapped?

In my app i have a scroll-view and a table-View added as a sub-view and on this scroll view i have multiple menu buttons (UI Button) added as sub-view to my scroll-view.i want to load the table-View with different arrays depending on respective menu button is tapped.Any Ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
[self PutButtoninScrollView:7];
}
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
myButton1=[[UIButton alloc]init];
myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
myButton1.frame=CGRectMake(5, 5, 70, 30);
[myButton1 setTitle:#"दुनिया" forState:UIControlStateNormal];
[myButton1 setTag:1];
myButton1.backgroundColor=[UIColor blackColor];
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton1];
myButton2=[[UIButton alloc]init];
myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2.frame=CGRectMake(80, 5, 70, 30);
[myButton2 setTitle:#"India" forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton2];
[self.myScrollView setContentSize:CGSizeMake(160, 35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([myButton.currentTitle isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([myButton.currentTitle isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
problem is when i execute the code
"successful" is displayed. that means ListViewAction method is getting called but my if statement is never executed.
try this
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
xPosition=5;
for(int i=0;i<numberOfbuttons;i++)
{
myButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(xPosition, 5, 70, 30);
myButton.backgroundColor=[UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
[myButton setTag:i];
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton];
xPosition+=75;
}
[self.myScrollView setContentSize:CGSizeMake(xPosition,35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
- (void) buttonClicked:(id) sender
{
NSString *strTitle = [sender currentTitle];
// arrItem have list of array items
NextView *detailViewController = [[NextView alloc] initWithNibName:#"NextView" bundle:nil];
detailViewController.arrTableView =[arrItem objectAtIndex:[strTitle integerValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([[sender currentTitle] isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([[sender currentTitle] isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
try this
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];

Resources