-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = #"enter name";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"icon_chapter.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)refersh:(id)sender{
}
above code shows a textfield with button at its right corner but when i clicked on it simulator crash. i can't understand whats the region behind it. any body tell where i'm wrong
Your method is spelled -refersh: while you send #selector(refresh:). Is it the typo?
Also, you seem to be leaking the UITextField, which is quite easy to fix:
[self.view addSubview:[txt autorelease]];
Related
When I click inside text box, How can I add plus button to UITextField as shown in the following screen? When this button is clicked it would launch iPhone contact application. I would very much appreciate any code example. Thanks
Following code use for adding button in UITextField.
-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = #"Enter or Mobile Number";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"crossImg.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:#selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)clearText:(id)sender{
}
You can use the below code for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == yourFirstTextField)
{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
}
}
You can use the above method if you added the field in IB.
If you are created it through code, you need to add the below code when creating:
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
Use following method for add UIButton
[self.txtField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (BOOL) textFieldDidChange:(UITextField *)textField
{
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(self.txtTag.bounds.size.width - 50, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtTag addSubview:btnColor];
return YES;
}
Or Write
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250);
[UIView commitAnimations];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(150, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtField addSubview:btnColor];
return YES;
}
Use rightView property of UITextField.
In textFieldDidBeginEditing: delegate create button with action you want & set to rightView.
And in textFieldDidEndEditing: set nil to rightView
you can also go for custom view ant then add sub view to it. then you hide it. it will be visible only when the user taps it
self.locationName.clearButtonMode = UITextFieldViewModeWhileEditing;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 30, 30);
[button setImage:[UIImage imageNamed:#"goButtonIcon.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.locationName.rightView = button;
self.locationName.rightViewMode = UITextFieldViewModeUnlessEditing;
and then call this method:
-(IBAction)goButtonClicked:(id)sender{
}
self.locationName -------> UITextField Reference (in ViewDidload you can write this code)
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];
i am having this code to show the button as the title of the navbar,and then in button code i write the code to change the font of the unbutton text label.but i didn't get the correct font version with the below code.
UIButton *titleButton = [[UIButton alloc]init];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:[NSString stringWithFormat:#"%# %#",delegatee.selectedBook,delegatee.selectedChapter]forState:UIControlStateNormal];
titleButton.titleLabel.font =[UIFont fontWithName:#"Georgia" size:10.0f];
[titleButton addTarget:self action:#selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
iput georgia but i didn't get the correct font
Thanks.
Try this :
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:[NSString stringWithFormat:#"%# %#",delegatee.selectedBook,delegatee.selectedChapter]forState:UIControlStateNormal];
titleButton.titleLabel.font =[UIFont fontWithName:#"Georgia" size:10.0f];
[titleButton addTarget:self action:#selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
try this
for button :- use in Navigation Title view , this is work IOS 4 and IOS 5 ,I use .....
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:#"dee"forState:UIControlStateNormal];
UIFont *font=[UIFont fontWithName:#"A_Nefel_Sereke" size:21];
titleButton.titleLabel.font =font;
titleButton.titleLabel.textColor = [UIColor redColor];
[titleButton addTarget:self action:#selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;
I think you have problem in your font name ... Please check
Why don't you just put a label instead of a button as the title ?
Here's code for a title label:
UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView)
{
titleView = [[UILabel alloc] initWithFrame:CGRectZero];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView setFont:[UIFont fontWithName:#"Georgia" size:10.0f]];
[self.navigationItem setTitleView:titleView];
[titleView release];
}
titleView.text = title;
If you need it to be UIButton, it shouldn't be too hard to adjust the code to your needs.
Good luck !
I am write code for scrollview and add button on it but button not appear in scrollview.
My code
tableProductScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(75, 442, 874, 208)];
tableProductScrollView.delegate=self;
[tableProductScrollView setBackgroundColor:[UIColor clearColor]];
[tableProductScrollView setCanCancelContentTouches:YES];
tableProductScrollView .indicatorStyle = UIScrollViewIndicatorStyleBlack;
tableProductScrollView .pagingEnabled = YES;
tableProductScrollView.showsVerticalScrollIndicator = YES;
[tableProductScrollView setContentSize:CGSizeMake(1748,208)];
[tableProductScrollView setScrollEnabled:YES];
[self.view addSubview:tableProductScrollView];
CGRect frameProduct1 = CGRectMake(85, 445, 142, 150);
tableProduct1Button = [UIButton buttonWithType:UIButtonTypeCustom];
tableProduct1Button.frame = frameProduct1;
UIImage *imgProduct1= [UIImage imageNamed:#"product6-small.png"];
[tableProduct1Button setImage:imgProduct1 forState:UIControlStateNormal];
tableProduct1Button.backgroundColor = [UIColor clearColor];
tableProduct1Button.highlighted = YES;
//---add the action handler and set current class as target---
[tableProduct1Button addTarget:self
action:#selector(homeButtonAction)
forControlEvents:UIControlEventTouchUpInside];
[tableProductScrollView addSubview: tableProduct1Button];
please give me suggestion.
maybe:
UIButton *tableProduct1Button =
[UIButton
buttonWithType:UIButtonTypeCustom];
Try to make a round rect button with some color and then see if you are able to see the button or else check the frame. Or try to set an image for the highlighted state.
[tableProduct1Button setImage:image1 forState:UIControlStateHighlighted];
I'm just wondering if anyone could help me change the position of a button label for a button that I've made with code.
Thanks a lot,
Chris
If you mean UIButton, look at UIButton's titleEdgeInsets property.
So the button label is an instance of UILabel, isn't it ?? Set the position of that label by setting the frame to it with respect to the UIButton frame.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, w, h)];
[titleLabel setText:#"TITLE"];
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];
I hard coded the x and y positions of the button as well as for the label. You set some other value's and place the label in the respective position where ever you want.
If you want to set only the text position of that label you can do it like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(3, 20, w, h)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)];
[titleLabel setText:#"TITLE"];
[titleLabel setTextAlignment:UITextAlignmentCenter];//(UITextAlignmentRight/UITextAlignmentLeft)
[button addSubview:titleLabel];
[self.view addSubview:button];
[titleLabel release];
Hope it will help you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,50,32);
[button setBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
[backButtonView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
//barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
[self.navigationItem setLeftBarButtonItem:barButtonItem];