Is there anyway to add same scroll menubar at the navigation bar? - ios

I have already posted my this problem another time but i have not got my answer perfectly.Here i am going to explain my problem another time, it is very important for me so at any cost i have to solve it.Now my problem is...
Suppose, I have 4 tabbaritem in a tabbarController and items "dashboard","order","product","customer".
every item of these tabbar is a calling there respective uiviewcontroller.
dashboar calling "DashboarViewController";
order calling "orderViewController";
product calling "ProductViewController";
customer calling "CustomerViewController";
Now, i have to set a scrolling menubar at every uiviewcontroller and this menu bar containing 4 buttons. These button names are same as tabbar items name "dashboard","order","product","customer".
Now when i press the button of the menu bar then respective controller will show same as for tabbar items. suppose i am pressing "order" tabbar item then it will show the "orderviewcontroller". when i will see this view controller it will also show me that menu bar at the top of the viewcontroller.Now, if i am click "product" button in this "orderviewcontroller" then it will sent back to me "productViewcontroller".
thats mean tabbar item and button of the scroll menubar will work same.
still now i have done these, my previous post image
How can i make same button in multiple view controller?
If some know how can do that then please explain it step by step.I do not need to give any code from you.just explain it step by step how can i do that after reading my previous post
Thanks In Advance.

Ha ha ha .....it was so much fun when i solved it.whatever i solved this problem in different way, i did not use scrollview button controller for controller just, in every controller i have made function to where buttons within a scrollview create and on action of button i have just change the selected index of the tabbar controller.
in -(void)viewDidload i wrote this code
UIView *scrollViewBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
scrollViewBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"topmenu_bg.png"]];
menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;
[scrollViewBackgroundView addSubview:menuScrollView];
[self.view addSubview:scrollViewBackgroundView];
[self createMenuWithButtonSize:CGSizeMake(92.0, 30.0) withOffset:5.0f noOfButtons:7];
here is the button create and action
-(void)mybuttons:(id)sender{
NSLog(#"mybuttons called");
UIButton *button=(UIButton *)sender;
NSLog(#"button clicked is : %iBut \n\n",button.tag);
int m = button.tag;
for(int j=0;j<8;j++){
if(button.tag == m){
self.tabBarController.selectedIndex = m;
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_hover.png"] forState:UIControlStateHighlighted]; //sets the background Image]
}
if(button.tag != m){
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
}
}
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
NSLog(#"inserting into the function for menu bar button creation");
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[button addTarget:self action:#selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
(button).titleLabel.font = [UIFont fontWithName:#"Arial" size:12];
if(i==0){
[button setTitle:[NSString stringWithFormat:#"Dashboard"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_hover.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==1){
[button setTitle:[NSString stringWithFormat:#"Order"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==2){
[button setTitle:[NSString stringWithFormat:#"Product"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==3){
[button setTitle:[NSString stringWithFormat:#"Customers"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==4){
[button setTitle:[NSString stringWithFormat:#"Content"] forState:UIControlStateNormal];//with title
}
if(i==5){
[button setTitle:[NSString stringWithFormat:#"Site Analysis"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==6){
[button setTitle:[NSString stringWithFormat:#"Store Settings"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
if(i==7){
[button setTitle:[NSString stringWithFormat:#"CMS Settings"] forState:UIControlStateNormal];//with title
[button setBackgroundImage:[UIImage imageNamed:#"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
}
button.frame = CGRectMake(i*(offset+buttonSize.width), 6.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 5;//half of the width
button.layer.borderColor=[UIColor clearColor].CGColor;
button.layer.borderWidth=0.0f;
button.tag=i;
[menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];
}

Related

How I can add action to title of NavigationBar?

I want to give/add action to the title of navigation title.
(Example "Explore")
I want to perform some activity using this approach.
I tried following code, but it's not working.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
//[view addSubview:button];
[self.navigationItem.titleView addSubview:button];
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
ViewController name is came from presentViewController type not from push.navigationController stack.
How I can achieve this using objective-C code?
Replace your code with the following:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
self.navigationItem.titleView = button;
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
titleView is nil by default.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview
You should set your title view with your button. Not add it as a subview.
self.navigationItem.titleView = button

Make navigation bar title clickable Objective C iOS 10.2

Is there any way to make the navigation bar title clickable? I don't have any specific class for navigation bar. I control everything from ViewController. If there's a way, I would love to know. I have been searching it for quite a white but couldn't find a suitable answer.
Make button on Navigation bar
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(btnclick:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 64.0);
self.navigationItem.titleView =button;
-(void)btnclick:(id)sender
{ NSLog(#"button click");
}
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender
{
NSLog(#"button clicked");
}

How to change title color and background color of dynamic UIButton when clicked and first button should be selected as default?

I have a view and I created some 8 dynamic buttons programmatically in that. The title color of buttons is white color. I want to change color of button title to green color when it is clicked. And if i click any other button, the previously clicked button title color should become white and current button title color should become green.
I have used the following code and its working properly for changing the color of previous button and now I want the first button to be in selected mode as default and first button should be changed when another button is clicked.
- (void)viewDidLoad
{
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[#"Exam Name"] = #"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray
alloc]initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListD
ict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
for (i = 0; i <[_examsListArray count]; i++)
{
aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:#"Exam Name"]
forState:UIControlStateNormal];
[aButton setTag:i];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f
blue:221.0f/255.0f alpha:1]];
[aButton addTarget:self action:#selector(whatever:)
forControlEvents:UIControlEventTouchUpInside];
[_buttonsHorizontalScrollView addSubview:aButton];
xCoord += buttonWidth + buffer;
}
[_buttonsHorizontalScrollView
setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender
{
UIButton *tempBtn = (UIButton *)sender;
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f
blue:221.0f/255.0f alpha:1]];
aButton = tempBtn;
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f
blue:83.0f/255.0f alpha:1]];
}
No I just want the first button to be selected as default when the view is loaded and whenever I select the other button it should change to deselected color.
if ([aButton tag] == 0)
{
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f
blue:83.0f/255.0f alpha:1]];
}
I have used the above code to select as first button as default but first button is not getting changed when other button is clicked and it is changing only when the first button is clicked again due to this aButton = tempBtn.How to do that?
I suggest you to set the selected status title color of all your buttons and then add them to an array. Then when you press one of them just change their status, no need to handle colors or keep track of the currently selected button.
#property (strong, nonatomic) NSMutableArray *yourButtons;
- (void)viewDidLoad {
[super viewDidLoad];
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[#"Exam Name"] = #"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray alloc] initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListDict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
// Generate background images
//
UIImage *backgroundImage = [self imageWithColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f blue:221.0f/255.0f alpha:1]];
UIImage *selectedBackgroundImage = [self imageWithColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f blue:83.0f/255.0f alpha:1]];
// Initialize your array
//
self.yourButtons = [[NSMutableArray alloc] init];
for (i = 0; i < [_examsListArray count]; i++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:#"Exam Name"] forState:UIControlStateNormal];
[aButton setTag:i];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[aButton setBackgroundImage:backgroundImage forState:UIControlStateNormal]
[aButton setBackgroundImage:selectedColorImage forState:UIControlStateSelected]
[aButton addTarget:self action:#selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[_buttonsHorizontalScrollView addSubview:aButton];
xCoord += buttonWidth + buffer;
// Select the first one
//
if (i == 0)
aButton.selected = YES;
// Add your newly created button to the array
//
[self.yourButtons addObject:aButton];
}
[_buttonsHorizontalScrollView setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender {
for (UIButton *button in self.yourButtons) {
// Toggle selected flag
//
button.selected = button == sender;
}
}
+ (UIImage *)imageWithColor:(UIColor *)color {
// http://stackoverflow.com/a/24665476/1835155
//
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
TRUEThe unpressed button is UIControlStateNormal while the pressed button is UIControlStateSelected so set the colours you want based on the State. You typically do this in ViewDidload before anyone has a chance to use it - Below is a sample setup:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateDisabled];
I believe what you should be doing when the trigger button is clicked is to just set the state to selected
- (IBAction)touchDown:(id)sender {
if (sender == trigger_button) {
[button setHighlighted:TRUE];
[button setSelected:TRUE];
}
In order to change the first button state when another button is clicked you have to "fake" a touch event. In the selector method of the other button do this:
For Example:
-(void)thirdButtonClicked:(id) sender
{
[firstButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}

Checkbox control for iOS application

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.
You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}
//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}
This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

Highlight the UIButtons created Dynamically?

In my application I have created 20 buttons with in a scroll view, now problem is that I was not able to highlight the selected button.
My intention is to show the pressed button with a different look than normal. When another button is pressed the previous one should become normal:
UIButton *Abutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[Abutton setTag:i-1];
Abutton.frame = CGRectMake(30.0, 0+j, 40.0, 40.0);
[Abutton setTitle:#"" forState:UIControlStateNormal];
Abutton.backgroundColor = [UIColor clearColor];
[Abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"image1.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"image2.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[Abutton addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:Abutton];
Finally I created the method for Abutton pressed as below:
-(IBAction)buttonpressed:(id)sender{
Abutton.highlighted=YES;
//.....
//.....
}
If do it like this then only the last button created dynamically gets highlighted. That's not exactly what I wanted.
I think you should replace your current code for the button pressed:
-(IBAction)buttonpressed:(id)sender{
UIButton *b = (UIButton *)sender;
b.highlighted = YES;
//.....
//.....
}
In your example you are specifically highlighting "AButton". This code highlights the button being pressed.
Solution 1:
Create an NSSet with references to all the buttons. In your buttonPressed method, call makeObjectsPerformSelector on the NSSet's buttons, setting them to the unhighlighted state.
Solution 2:
Use a UISegmentedControl. That seems like what you should be doing in this case anyway.

Resources