I Am using custom Tab bar with buttons.I have made custom tab bar at the top position.I have successfully made it.But I want tab bar position to be in the center for two view controller>how can i do it.Is it possible??
Here 's my code
This is my custom tab bar.m file
-(void)addCustomElements
{
btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn1.frame = CGRectMake(0, 264, 65, 40); // Set the frame (size and position) of the button)
[btn1 setBackgroundColor:[UIColor darkGrayColor]];
[btn1.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:16.0]];
[btn1 setTitle:#"Info" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn1 setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn1 setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially
// Now we repeat the process for the other buttons
btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn2.frame = CGRectMake(64, 264, 65, 40); // Set the frame (size and position) of the button)
[btn2 setBackgroundColor:[UIColor darkGrayColor]];
[btn2.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:16.0]];
[btn2 setTitle:#"Book" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn2 setTag:1]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn2 setSelected:true];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn3.frame = CGRectMake(128, 264, 65, 40); // Set the frame (size and position) of the button)
[btn3 setBackgroundColor:[UIColor darkGrayColor]];
[btn3.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:16.0]];
[btn3 setTitle:#"Menu" forState:UIControlStateNormal];
[btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn3 setTag:2]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn3 setSelected:true];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn4.frame = CGRectMake(193, 264, 65, 40); // Set the frame (size and position) of the button)
[btn4.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:16.0]];
[btn4 setBackgroundColor:[UIColor darkGrayColor]];
[btn4 setTitle:#"Reviews" forState:UIControlStateNormal];
[btn4 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn4 setTag:3]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn4 setSelected:true];
btn5 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn5.frame = CGRectMake(258, 264, 65, 40); // Set the frame (size and position) of the button)
[btn5.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:16.0]];
[btn5 setBackgroundColor:[UIColor darkGrayColor]];
[btn5 setTitle:#"Map" forState:UIControlStateNormal];
[btn5 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn5 setTag:4]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
[btn5 setSelected:true];
// Add my new buttons to the view
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];
[self.view addSubview:btn5];
// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
[btn1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn5 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonClicked:(id)sender
{
int tagNum = [sender tag];
[self selectTab:tagNum];
}
-(void)selectTab:(int)tabID
{
switch(tabID)
{
case 0:
[btn1 setSelected:true];
[btn2 setSelected:false];
[btn3 setSelected:false];
[btn4 setSelected:false];
[btn5 setSelected:false];
break;
case 1:
[btn1 setSelected:false];
[btn2 setSelected:true];
[btn3 setSelected:false];
[btn4 setSelected:false];
[btn5 setSelected:false];
break;
case 2:
[btn1 setSelected:false];
[btn2 setSelected:false];
[btn3 setSelected:true];
[btn4 setSelected:false];
[btn5 setSelected:false];
break;
case 3:
[btn1 setSelected:false];
[btn2 setSelected:false];
[btn3 setSelected:false];
[btn4 setSelected:true];
[btn5 setSelected:false];
break;
case 4:
[btn1 setSelected:false];
[btn2 setSelected:false];
[btn3 setSelected:false];
[btn4 setSelected:false];
[btn5 setSelected:true];
break;
}
if (self.selectedIndex == tabID) {
UINavigationController *navController = (UINavigationController *)[self selectedViewController];
[navController popToRootViewControllerAnimated:YES];
} else {
self.selectedIndex = tabID;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self hideExistingTabBar];
[self addCustomElements];
// Do any additional setup after loading the view.
}
Please Help.Thanks In advance!!
you can try this way,Here is the link and source code,in this you can change the positions of Tabs
https://github.com/hollance/MHTabBarController
Hope,this helps you.
Related
I want to make an app for iphone. It is a Nonogram something similar to this: http://www.puzzle-nonograms.com/ How can I make the UIButtons changes their color from white to black and reverse?
.h file
UIButton *button;
BOOL clicked;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
clicked=YES;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(changeBtnColor)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#" 1 " forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)changeBtnColor{
if (clicked) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
clicked=NO;
}
else{
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
clicked=YES;
}
}
I hope it will help. Best of luck..
No need to ivar a boolean, buttons have selected, highlighted and normal states built into them.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 320, 50)];
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Normal State" forState:UIControlStateNormal];
[button setTitle:#"Selected State" forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[self.view addSubview:button];
}
-(void)buttonPress:(UIButton *)button{
button.selected = !button.selected;
//do your other code here for button selection
}
Otherwise set up the button states inside interface builder and just have the buttonPress selector handle the state change.
Currently i am working in iPhone application, I have create two button and set image for both buttons, when the user touch button2 then show button1 image inside button2, i tried my level best, but i can't fix this, please any one help me
Thanks in Advance
I tried this:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"4.png"] forState:UIControlStateNormal];
btn1.frame=CGRectMake(61, 60, 50, 50);
[btn1 addTarget:self action:#selector(Button1Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"8.png"] forState:UIControlStateNormal];
btn2.frame=CGRectMake(112, 60, 50, 50);
[btn2 addTarget:self action:#selector(Button2Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
-(void)Button2Method
{
[btn2 setImage:[UIImage imageNamed:btn1.currentBackgroundImage] forState:UIControlStateNormal];
}
Try this:-
[btn2 setImage:btn1.currentImage forState:UIControlStateNormal];
And currentBackgroundImage is an UIImage and you're treating it as a NSString this is wrong.
Use this
UIImage *img = btn1.currentImage;
[btn2 setImage:img forState:UIControlStateNormal];
This is because you are setting image to button btn1 setImage: and accessing background image btn1.currentBackgroundImage
so change your code Tested
-(void)Button2Method
{
[btn2 setImage:btn1.imageView.image forState:UIControlStateNormal];
}
I am new to iPhone,
I want to implement radio button in my app, there are three buttons in my app, button should behave like radio button.
Here is my code snippet,
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateSelected];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
- (IBAction)RadioButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Any help will be appreciated.
You can use following code.
//Add all the buttons to class level NSMutable array
UIButton *Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn1.frame=CGRectMake(10, 190, 20, 20);
[Btn1 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn1 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn1];
[self.buttonsArray addObject:Btn1];
UIButton *Btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn2.frame=CGRectMake(10, 240, 20, 20);
[Btn2 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn2 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn2];
[self.buttonsArray addObject:Btn2];
UIButton *Btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
Btn3.frame=CGRectMake(10, 290, 20, 20);
[Btn3 setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
[Btn3 addTarget:self action:#selector(RadioButton:) forControlEvents:UIControlEventTouchUpInside];
[scrollVw addSubview:Btn3];
[self.buttonsArray addObject:Btn3];
- (IBAction)RadioButton:(id)sender{
[self resetAllButtons];
UIButton* button=(UIButton*) sender;
[button setImage:[UIImage imageNamed:#"radio-on.png"] forState:UIControlStateNormal];
}
-(void) resetAllButtons{
for(int i=0;i<[self.buttonsArray count];i++){
UIButton* button=[self.buttonsArray objectAtIndex:i];
[button setImage:[UIImage imageNamed:#"radio-off.png"] forState:UIControlStateNormal];
}
}
You are checking all views in self.view.subviews but the buttons are actually added to the UIScrollView (I guess) called scrollVw.
Change the for loop to
for (UIButton *Radiobutton in [self.scrollVw subviews]) {
if the scrollVw is added as a property in the .h file.
Any opposition to using a UISegmentedControl? It works like a radio button, in that it can only have one option selected at a time. If you are deadset on the appearance of radio buttons I would subclass it to change the appearance.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
Alternatively you could simply use the iOS provided Control for something like that:
UISegmentedControl
It can easily handle three segments, is quite flexible and can behave like a radio button where you can select only one segment or multiple.
i am creating an application which includes dynamic pages. actually, i created some dynamic questions and UIButtons (custom checkBox) by checking the responses from the server.
my code is here,
NSString *response=[ResponseFromServer ObjectAtIndex:0];
if ([comSt compare: #"CheckBoxList"]==NSOrderedSame)
{
int count=[ResponseFromServer count];
for(int i=0;i<count;i++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=i;
[button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
[button setCenter:CGPointMake(116.0,p1)];
[button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[button addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
p1=p1+30;
}
-(void)checkButtonTapped:(id)sender
{
int tagVal = [(UIButton*)sender tag];
if (tagVal==0) {
NSLog(#"tag = 0");
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"check.png" forState:UIControlStateSelected]; //not working, button image is not changing
}
assume i have created 5 dynamic buttons with different tag. i set the image for all those button to uncheck.png first. it works.
but when i clicked a specific button and need to change that button image to checked.png, its not working on the button action.
i need to change it like checkBox control, how will i access the control of a dynamically created button from a group of buttons?
i think u can understand my requirement, thanks in advance..
While adding dynamic button set [button setSelected:NO];
Replace method and Add one method given below
-(void)checkButtonTapped:(id)sender
{
UIButton *curButton = (UIButton*)sender
if (curButton.tag==0)
{
[self buttonAction:curButton];
}
if (curButton.tag==1)
{
[self buttonAction:curButton];
}
// continues
}
-(void)buttonAction:(UIButton *)curButton
{
if(![curButton isSelected])
{
[curButton setSelected:YES];
[curButton setImage:[UIImage imageNamed:#"check.png" forState:UIControlStateSelected]; //not working, button image is not changing
} else {
[curButton setSelected:NO];
[curButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
}
In your code you need to change like this
Try this code
if ([comSt compare: #"CheckBoxList"]==NSOrderedSame)
{
int count=[ResponseFromServer count];
for(int i=0;i<count;i++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=0;
[button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
[button setCenter:CGPointMake(116.0,p1)];
[button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[button addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
p1=p1+30;
}
}
-(void)checkButtonTapped:(id)sender
{
int tagVal = [(UIButton*)sender tag];
if (tagVal==0) {
NSLog(#"tag = 0");
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"check.png" forState:UIControlStateSelected];
[button setTag:100];
}
else
{
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"uncheck.png" forState:UIControlStateSelected];
[button setTag:0];
}
}
Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.
UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
[viewContainer addSubview:contentView];
return contentView;
- (void)buttonClicked
{
NSLog(#"button clicked");
}
I think labels recives touches instead of button.
Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Try changing the top part of your code to this:
CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];
//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
It may very well be the following:
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
im sure you would need to add them in the following order:
[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];
This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.
Check out the UIView Documentation here
the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.
By default the last subView captures all touch events and doesn't pass them on.
Add the colon in last of your method in selector
[connectedStories addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:#"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:#"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];