How to draw small button inside big button in iOS - iphone

I made a button already. When I click it, it becomes big but letter is not in good position. I want to put another button inside the big button. How can I do this ?

If you just wish to add a second button as a subview of the first button, you can use this:
UIButton *mySmallButton = [[UIButton alloc] initWithFrame:CGRectMake:(0, 0, 50, 50)];
[mySmallButton setBackgroundColor:[UIColor redColor]];
[myBigButton addSubview:mySmallButton];
However, if you're just looking to modify the text size/alignment of the first button once it has grown you can use these:
[[myBigButton titleLabel] setTextAlignment:UITextAlignmentCenter];
[[myBigButton titleLabel] setFont:[UIFont systemFontOfSize:24]];

Related

UIControl track touches outside its view

I'm building a menu as a subclass of UIControl that opens when a user touches it and will close if a user doesn't choose one of its options.
I'm looking for a way to track when a user touches outside so that I can close the menu
Put one view behind your menu that cover whole area of your device with background of clear color. when your menu is open make that view visible. as it has same height and width as your device whenever you click outside the menu you can identify that view using TapGesture or make it UIControl. than call method to hide both your menu and that uiview.
Hope this will help.
What I really like to do is to make a big button that covers the entire view, and have it below the menu, or whatever else I want to touch away from and have something happen.
UIButton *bigBackButton = [[UIButton alloc]initWithFrame:self.view.frame];
[bigBackButton addTarget:self action:#selector(backButtonSelected) forControlEvents:UIControlEventTouchDown];
[bigBackButton setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.view addSubview:bigBackButton];
}
- (void)backButtonSelected:(UIButton *)button {
//get rid of the button
[button removeFromSuperview];
//do whatever else you need to do
[yourmenu dosomething];
}
on some apps, I'll make the button dark and translucent to highlight the menu, whatever else. Other times I'll make it pretty much invisible, like [UIColor colorWithWhite:1.0 alpha:0.01];
in your menu routine you could add these same routines. You could pass the frame of the parent view, or just look it up in the layoutSubviews routine, if your menu is a sublcass of a UIView.
in your layoutSubviews, add this:
UIButton *bigBackButton = [[UIButton alloc]initWithFrame:self.superview.frame];
[bigBackButton addTarget:self action:#selector(backButtonSelected) forControlEvents:UIControlEventTouchDown];
[bigBackButton setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.view addSubview:bigBackButton];
and then put the backButtonSelected method in your menu class as well.

UIButton touchUpInside event not firing correctly

I have a tableview that contains a row with a custom cell that contains a UIButton. However, the button doesn't always fire the action. Here's my code:
submitButton = [[UIButton alloc] init];
[[submitButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[submitButton setClipsToBounds: YES];
submitButton.backgroundColor = [UIColor grayColor];
[submitButton setTitle:#"Send" forState:UIControlStateNormal];
[self.contentView addSubview:submitButton];
[submitButton addTarget:self action:#selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[submitButton release];
This is called in the custom cell's -(id)initWithStyle:
The buttonAction method looks like this:
-(void)buttonAction
{
NSLog(#"Button Clicked!");
}
It seems that the only way I can get the buttonAction to fire is if I press down on the button and release somewhere inside the cell's frame, but not inside the button itself. Why would that be?
*UPDATE*
Problem still exists, but I found that the more consistent way to get the button to fire is to click and drag to the left or right and then let go, as long as I let go within the bounds of the cell/row.
UPDATE #2
It looks like if I use iOS 6.0, it works as intended. But on 5.0 or 5.1 it does not.
Try assigning the same method call to TouchUpOutside as well - you should then see it work every time. TouchUpInside is only fired if you lift your finger while still within the bounds of the button.
Try [submitButton sizeToFit]. I'm wondering whether your button has any size (since I don't see you giving it any).
Also: Create your button with [UIButton buttonWithType: UIButtonTypeCustom] instead of alloc-init.

In iOS, can a UIButton be added by alloc and initWithFrame?

A button can be added by:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100, 100, 160, 50);
[myButton setTitle:#"click me" forState:UIControlStateNormal];
[self.view addSubview:myButton];
but can we go with the initWithFrame method:
UIButton *myButton2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 160, 50)];
//myButton2.buttonType = UIButtonTypeRoundedRect;
myButton2.titleLabel.text = #"click me";
[self.view addSubview:myButton2];
but line 2 above is commented out because it causes an error for a "read only" property, and with that line commented out, still no button shows up at all... can a UIButton be added if using initWithFrame to begin with?
(update: I set a yellow background to see the view's area, and found that the label text of button 2 actually shows up as "click me" in white... but touching it has no visual effect... so I wonder how to change code sample 2 to make it work...)
Yes of course it can. I got into a little bit of a discussion as to that readonly buttonType property a while back. The general concensus is that there is neither a safe way, nor a good reason, for anyone to switch the type of a button. And besides, +buttonWithType returns an instance of UIButton just as well as +Alloc and -init do, so it isn't a problem.

Where to insert code that creates buttons programmatically?

I receive some JSON from the net and based on that data, I must create 2 or 3 buttons. Part of my gui is static and created in NIB (won't change), only the number of buttons will change. I found this code for making buttons in code:
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
Is this the right way? In which method of my viewcontroller should I put this code?
You can add the button whenever you want, as long as the view has been loaded already. The one thing you'd need to add to the above code is
[[self view] addSubview:button];
Using this code, you have a button on the screen, but it won't be able to trigger any actions. You'll probably also want to add:
[button addTarget:self action:#selector(someMethod:) forControlState:UIControlEventTouchUpInside];
You should add the buttons in the delegate/method that parses the JSON data.
Don't forget to add the created buttons to your view:
[containerView addSubview:button];

In iPhone application can we add any such key to the keybord which when pressed will remove the keyboard from the view?

I have a table view in which there are almost 16 text boxes where in i am taking input from the user but once the we are done with the editing of the textboxes the keyboard should hide which is not happening.I saw in one application where we can have an costume keyboard and add an key/button to it on click of which the key board will get removed from the view?any one have any idea...
Setup textfield like the following code, particularly add event on UIControlEventEditingDidEndOnExit and set returnKeyType to UIReturnKeyDone
//username text
UITextField* usernameTF=[[[UITextField alloc] initWithFrame:CGRectMake(90,40,210,31)] autorelease];
usernameTF.placeholder=#"Username";
[usernameTF setFont:[UIFont systemFontOfSize:20.0]];
[usernameTF setBackgroundColor:[UIColor clearColor]];
usernameTF.clearsOnBeginEditing=TRUE;
usernameTF.enablesReturnKeyAutomatically==TRUE;
usernameTF.clearButtonMode=TRUE;
[usernameTF setTextColor:[UIColor grayColor]];
[usernameTF setBorderStyle:UITextBorderStyleRoundedRect];
usernameTF.contentHorizontalAlignment=TRUE;
usernameTF.returnKeyType=UIReturnKeyDone;
[usernameTF addTarget:self action:#selector(textOver:) forControlEvents:UIControlEventEditingDidEndOnExit];
and then on selector action implement this method-
-(IBAction)textOver:(id)sender{
[sender resignFirstResponder];
}
if you want to add done button to numeric keypad then check out the following link
http://www.neoos.ch/news/46-development/54-uikeyboardtypenumberpad-and-the-missing-return-key
it works for me.
let me know if you have any problem.