Button with hidden menu iPhone - iphone

I need to implement a single button which would be shown at upper right corner of the application on UIView or MKMapView. On clicking that button a combo should come up and user would be able to select the categories.
How can I achieve that?

You have to create an UIButton and add it as a subview of your UIView (for example in viewDidLoad method if your view is linked to an UIViewController).
UIButton *showButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showButton.frame = CGRectMake(500, 20, 150, 44); // hardcoded frame, not quite elegant but works if you know the dimension of your superview
[showButton setTitle:#"Show Categories" forState:UIControlStateNormal];
// add target and actions
[showButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a superview, your parent view
[superView addSubview:showButton];
Then you add a method, called buttonClicked: that takes an id parameter (usually the sender, showButton in this case).
-(void)buttonClicked:(id)sender
{
// visualize categories
}
To visualize the categories you can follow two different ways:
Present a UITableViewController inside a UIPopoverController (only for iPad device)
Show a modal controller presenting a UITableViewController (both iPad and iPhone devices).
The UITableViewController allows you to have a list of categories and then select one of them.
P.S. Check the code in XCode because I've written by hand (without XCode)

Related

Implementing custom UITabBarController without private APIs

After searching through several question on StackOverflow I've found out that there is only 1 major project for creating custom UITabBar called BCTabBarController. The description to it says:
There are several problems with using the standard UITabBarController
including:
It is too tall, especially in landscape mode
The height doesn't match the UIToolbar
It cannot be customized without using private APIs
Nevertheless, I've found this strange project on GitHub with the tutorial here that uses standard UITabBarController in its implementation with UIButtons for each tab and it's working (strangely enough, but it does).
I was wondering, if this is wrong to create your custom UITabBarController with UIButtons instead of tabs and what would it result into? The implementation of this looks like this:
- (void)viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideTabBar];
[self addCustomElements];
}
- (void)hideTabBar
{
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
view.hidden = YES;
break;
}
}
}
-(void)addCustomElements
{
// Initialise our two images
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
btn1.frame = CGRectMake(0, 430, 80, 50); // Set the frame (size and position) of the button)
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
btn1.backgroundColor = [UIColor yellowColor];
[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
In my project I will be using iOS 5.1 and up and no Storyboards or XIBs. Thanks!
Since iOS 5.0, it is no longer a problem to create your own UITabBarController using a line of UIButtons at the bottom of the screen.
In previous versions of the iOS SDK, it was a bit risky as you had to manage the forwarding of the viewWill/viewDidmethods by yourself.
Have a look at the UIViewController Class Reference, section Implementing a Container View Controller, you will find all you need there : UIViewController Class Reference
There is also a featured article explaining exactly what you need : Creating Custom Container View Controllers
Hope this will help,

Making an assignable grid to segue in xcode

I need to implement a grid view of images that requires each segment to segue to another view controller /or View.
My parameters:
I need 260 segments: approx 18px x 18px
Each segment will be numbered (1-260) and have a diffrent (background) image
One segment must be highlighted (a daily square like ical)
You can tap any segment to segue to the next view controller
I have looked at custom TVCells / Buttons but are there more options out there?
Thank you
In the end I opted for:
Linking the 1stViewController to the 2ndViewController in the
storyboard and naming the seque.identifier in the attributes.
Creating a grid of UIButtons in IB and assigning them individual tags in the attributes (1-260 - don't use 0 as it is a default).
To change the buttons' backgroundImage daily I set up a day-counter integer
and in the viewDidLoad of the 1stVC coded:
[(UIButton*)[self.view viewWithTag:dayCount] setBackgroundImage:[UIImage imageNamed:#"image_Day.png"] forState:UIControlStateNormal];
As there were multiple UIButtons, I decided that dragging them all to a IBAction in IB was too long a task so I assinged them programatically using:
-(void) assignButtons{
[(UIButton*)[self.view viewWithTag:1] addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[(UIButton*)[self.view viewWithTag:2] addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
} //etc for all 260
Then used performSegueWithIdentifier: in a method:
-(IBAction) buttonClicked:(id)sender{
[self performSegueWithIdentifier:#"mySegue" sender:self];
}

How to remove background image to a button

I added 10 buttons to a view (example view name is "menuView"), now I want to remove the background image for 2nd, 3rd, 4th buttons. I wrote the code like this
for(id btn in [menuView subViews]){
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
The problem with this code is, it is removing all 10 button's backGroundimage, but I need to set nil for the 2nd, 3rd, and 4th buttons
If you create a tag for the buttons that you add, you can filter against them.
for(UIButton *btn in [menuView subViews]){
if (btn.tag == 2 || btn.tag == 3 || btn.tag == 4) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
}
Of course, you need to make sure that there are no other views in menuView that could share the same tag. So the choices are to make the tags large, unique values, or checking that they are actually UIButtons. I've edited this assuming that the only subviews of menuView are UIButtons. Enumerating over UIButtons won't cause compiler warnings about tag not being a property of NSObject.
UIButton is a subclass of UIControl which is a subclass of UIView. UIView has the tag property, so UIButton inherits this property. It's useful to look at the docs for a class that you are using, and continue up the hierarchy to see if there are properties or methods that are useful for what you need to do.
Just to expand on my comment.
Using an IBOutletCollection which you can point an array to many objects in the nib. You declare this as such (synthesizing in the implementation):
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *threeButtons;
This says to IB that it's a collection of UIButton elements. In IB, you connect this to the three buttons you wish to remove the background image for, by control dragging from it to the buttons. Once this is done the array will contain those buttons you connected up and you can loop like so:
for (UIButton *button in self.threeButtons) {
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
Again, the link to a more detailed explanation can be found at: http://bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection
When creating the buttons, try using the "tag" property. Then, when you're setting the background to nil, you could check for btn.tag == 2,btn.tag == 3 or btn.tag == 4.
You could have assigned tags to the buttons from 1 to 10 when adding them to menu view. And now with the help of tags, we can decided what to do with buttons.
Firstly, are you placing the buttons using Interface Builder?
If so, I'd recommend placing numbered tags for each of the buttons and then you can use something like the following to find the appropriate buttons and remove the background image.
for(UIButton *buttonname in [yourView subViews]){
if (buttonname.tag == 2 || buttonname.tag == 3 || buttonname.tag == 4) {
[buttonname setBackgroundImage:nil forState:UIControlStateNormal];
}
}
If you're creating them programmatically and sequentially, I'd recommend placing the buttons in an array as they're made and just remove the background of the buttons using "objectAtIndex".

how to place menu button in iphone

i need to place menu button in iphone.
i have some touch with android.
In android i display like this by pressing menu.
Is it possible to handle iphone menu button.
if it is not where can i place these.
Thank u in advance.
use the round rect button and change the property into custom place the image as the background image of the button do this for more menu item in your home page.
this is the way i do the menu item.......
I think with Xcode and Interface Builder (iPhone's development platform), getting this task done would be even simpler.
As a brief introduction to iPhone app's UI design, to place this tab bar (as you put it, the menu) on a view, all you need to do is pick up a tab bar from the library and place it on a nib file's view. You can also connect such a tab bar with some codes responsible for internal logic by simply clicking and dragging. It's simple.
btw, a nib file is just some file format Xcode and Interface Builder use to hold the UI data.
iPhones don't have a dedicated slide out menu - everything is on the screen at all times, and there are no purpose built buttons (like back and home).
You should either always show your actions on the screen, or if they are unimportant or infrequent, hide them behind another action button (like a + or wrench icon).
You should use images same as menu items and put custom button over the images.This is the only way to show menu as you want.
Iphone sdk provides tabBar and toolBar, you also can use these in customize forms.This is the correct way to making menu.Iphone design pattern having lot of strengths over android so you can make every thing in Iphone easily but you cant make every thing in android same as iPhone.Iphone having international standards so must use iPhone's controls.
.h
IBOutlet UIScrollView *scrollView;
#property ( nonatomic , retain ) IBOutlet UIScrollView *scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender;
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons;
.m
#synthesize scrollView;
-(void)AppleVijayAtFacebookDotCom:(id)sender{
NSLog(#"AppleVijayAtFacebookDotCom called");
UIButton *button=(UIButton *)sender;
if (button.tag == 0) {
NSLog(#"hey have clicked first button, this is my tag : %i \n\n",button.tag);
}
else if (button.tag == 1) {
NSLog(#"hey have clicked second button, this is my tag : %i \n\n",button.tag);
}
// ......like this
NSLog(#"button clicked is : %iBut \n\n",button.tag);
}
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(AppleVijayAtFacebookDotCom:) forControlEvents:UIControlEventTouchUpInside];
//[button1 setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];//with image
//OR
[button setTitle:[NSString stringWithFormat:#"%iBut",i] forState:UIControlStateNormal];//with title
button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 10;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.backgroundColor=[UIColor blackColor].CGColor;
button.layer.borderWidth=2.0f;
button.tag=i;
[self.scrollView addSubview:button];
}
self.scrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
//self.navigationItem.titleView=self.scrollView;//if u have navigationcontroller then enable this line
}
Dont forget to connect the scrollView in interface builder
while creating the scrollview in IB make sure ur scrollView height is 44.which is default to navigation bar.so it will look nice.
in viewDidLoad call
[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:30];
OUTPUT

UITextField leftView

Is it possible to set up a UITextField with a leftView so that if a user clicks into the UITextField the keyboard shows but if they click on an icon in the leftView another method is called (i.e., one that displays a UIPickerview)?
Have you checked that the leftFieldViewMode is set to something other than never
[textField setLeftViewMode:UITextFieldViewModeAlways];
Instantiate a button as u do normaly
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30,30)];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
Instantiate a textfield as you do normally
UITextField *txtfield1=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,300,30)];
[txtfield1 setBackgroundColor:[UIColor grayColor]];
[txtfield1 setPlaceholder:#" Enter"];
To set a view in left side(there is right view also available)
[txtfield1 setLeftView:btn];
if u want the view to show always then below code does it
[txtfield1 setLeftViewMode:UITextFieldViewModeAlways];
if u want the view to show never then
[txtfield1 setLeftViewMode:UITextFieldViewModeNever];
if u want the view to show if the textfield is not edited then below
[txtfield1 setLeftViewMode:UITextFieldViewModeUnlessEditing];
if u want the view to show if the textfield is edited then
[txtfield1 setLeftViewMode:UITextFieldViewModeWhileEditing];
button click event and pop over
-(void)BtnClick
{
//Usual pop over coding goes here
}
Have you tried using a UIButton for this task? From the documentation for the leftView property:
If your overlay view does not overlap
any other sibling views, it receives
touch events like any other view. If
you specify a control for your view,
the control tracks and sends actions
as usual. If an overlay view overlaps
the clear button, however, the clear
button always takes precedence in
receiving events.
So create a UIButton instance, configure its appearance and actions as needed, then set it as the leftView property.