How to add a textfields dynamically on clicking the button?
when clicking on the button textfield should appear with scrollview.
can anyone explain me how to do?
what have you tried??
May be this is what you are asking for.
-(IBAction)buttonClicked:(id)sender
{
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
}
EDIT
IF not dynamically,
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 40, 21)];
[scrollView addSubview:textField];
[self.view addSubview:scrollView];
[scrollView setHidden:YES];
}
-(IBAction)buttonClicked:(id)sender
{
[scrollView setHidden:NO];
}
You can use below code to add Text Field programmatically..
UITextField * aTextfield = [[UITextField alloc] initWithFrame:frame]; // give your frame
aTextfield.borderStyle = UITextBorderStyleRoundedRect;
aTextfield.textColor = [UIColor blackColor];
aTextfield.font = [UIFont systemFontOfSize:17.0];
aTextfield.placeholder = #"Suchen";
aTextfield.backgroundColor = [UIColor clearColor];
aTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
aTextfield.keyboardType = UIKeyboardTypeDefault;
aTextfield.returnKeyType = UIReturnKeySearch;
aTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
aTextfield.delegate = self;
[scrollview addSubview:aTextfield];
In Button action, you can create the dynamic textfields and them to your scrollview.....
Take a look at my code.
http://rajneesh071.blogspot.in/2012/09/make-textfield-and-label-dynamically-in.html
According to this you can create number of textfield and label in scroll view, so you can change it according to your requirement.
Related
I tried to set the right side margin in UItextfield but I can't get success for this.
I tried below code:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ];
[lbl setText:#"Partenza:"];
[lbl setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
txtFCarat.delegate = self;
Please help me if any body have a solution for this!
You can override this below two methods
#implementation rightTextField
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset( self.bounds , margin position , margin position );
}
// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset( self.bounds , margin position ,margin position );
}
Edit: use to bind code like
textField.bounds = [self editingRectForBounds:textField.bounds];
textField.bounds = [self textRectForBounds:textField.bounds];
Your expected Answer:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];
textfield.rightView = imageView;
textfield.rightViewMode = UITextFieldViewModeAlways;
Add the below line where you are creating textfield..and set value as per requirement
textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
Another way to achieve this by overriding the textRectForBounds
Let me to give complete Description how to do this or achieve it
First you create a class and name "textField"(or what ever you Want) and must be sure that IT IS SUBCLASS OF UITextField.
2.Now open the textfield.m class and use this code or paste below code in this class (textfield):
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 50, bounds.origin.y,
bounds.size.width - 20, bounds.size.height);
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
If you want to customize the textfield more then you can use here only or in calling Class like
use the below code :Note it is only for testing purpose.it depend on your requirement
- (void)settingTextField {
[self setTextField];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
- (void)setTextField {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
3.improt textField.h class into your view-controller where you wants to create textfield
and you call it like this.
#import "textField.h"
//Viewcontroller Class
- (void)viewDidLoad
{
[super viewDidLoad];
textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
field1.backgroundColor=[UIColor greenColor];
field1.placeholder=#"0";
[self.view addSubview:field1];
}
Try to adjust the value textRectForBounds as per you reqitement
NSString *myString = #"Partenza:";
UIFont *myFont = [UIFont fontWithName:#"Verdana" size:12];
CGSize myStringSize = [myString sizeWithFont:myFont];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myStringSize.width + 10, 25) ];
[lbl setText:myString];
[lbl setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
I have UITextView in my iPhone app. In the UITextView i have added UIImageView as subview. When the entered text reaches the final height the texts are scrolling to top. Instead the UIImageView (with image) also scrolling top. How can i handle to stop the image scroll and allow the text to scroll? Here is my code for your reference,
messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
messageTextView.delegate = self;
messageTextView.backgroundColor = [UIColor clearColor];
messageTextView.clipsToBounds = YES;
messageTextView.font = [UIFont fontWithName:#"Helvetica" size:14];
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
textViewImageView = [[UIImageView alloc]initWithFrame: CGRectMake(0, 0, 210, 30)];
textViewImageView.image = [UIImage imageNamed: #"textbg.png"];
textViewImageView.contentMode = UIViewContentModeScaleToFill;
textViewImageView.contentStretch = CGRectMake(0.5, 0.5, 0, 0);
[messageTextView addSubview: textViewImageView];
[messageTextView sendSubviewToBack: textViewImageView];
[messageTextView addSubview:textViewLabel];
Can anyone please help me to solve this? Thanks in advance. Looking forward your reply.
have you considered to position the imageView behind the textView? Or is there any reason that forbids such a layout?
something like that:
UITextView *messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:messageTextView];
UIImageView *textViewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35, 5, 210, 30)];
// ...
textViewImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:textViewImageView];
[self.view sendSubviewToBack:textViewImageView];
Maybe this could help.
Use the scrollview delegate to reposition your image when it reaches the bounds.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
CGRect imageFrame = textViewImageView.frame;
imageFrame.origin.y = scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height);
textViewImageView.frame = imageFrame;
}
}
This should work:
[yourTxtView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"your image.png"]]];
I have created UIToolbar control.which is added as setInputAccessoryView to one UITextField Control.Which works fine. the tollbar comes up with uikeyboard when tapping UITextField.Up to this no issues.
When i placed one button, when tapping on it i want to hide the keyboard and only want to display the toolbar.
This is not happening.the toolbar is getting hidden.
Please let me know , what is the problem/ reason in the code.
Thanks for your time.
- (void)viewDidLoad {
[super viewDidLoad];
//
myToolbar = [[UIToolbar alloc] init];
[myToolbar sizeToFit];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
UIImage *image = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 100, image.size.width, 25 );
aButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[aButton setTitle:#"Tap" forState:UIControlStateNormal];
[aButton setBackgroundImage: image forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
aButton.titleLabel.font = [UIFont boldSystemFontOfSize:10];
[aButton addTarget:self action:#selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
myToField = [[UITextField alloc] init];
myToField.frame = CGRectMake(0, 0, 320, 48);
myToField.textColor = [UIColor blackColor]; //text color
myToField.font = [UIFont systemFontOfSize:17.0]; //font size
myToField.placeholder = #"To:"; //place holder
myToField.backgroundColor = [UIColor whiteColor]; //background color
myToField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myToField.keyboardType = UIKeyboardTypePhonePad; // type of the keyboard
myToField.returnKeyType = UIReturnKeyDone; // type of the return key
myToField.clearButtonMode = UITextFieldViewModeWhileEditing;
[myToField setInputAccessoryView:myToolbar];
myToField.textAlignment = UITextAlignmentLeft;
[myToField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
myToField.rightViewMode=UITextFieldViewModeAlways;
// has a clear 'x' button to the right
myToField.delegate = self;
[myToField becomeFirstResponder];
[self.view addSubview:myToField];
}
-(void)aCenterBtnClicked:(id)sender{
[myToField resignFirstResponder];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
}
inputAccessoryViews are placed as a subview on the UIKeyboard, if you hide the keyboard, it will hide the inputAccessoryView. Try adding the toolbar as a subview of self.view and animate it's position up when the keyboard is displayed by subscribing to the UIKeyboardWillShow notification and pulling the animation duration from the userInfo.
You try for static toolbar i think that will be easy for you.I am not done before remove from setinputview because there is no method for remove set input view.
I'm trying to add a button to a UIview, to cover the whole area of the view.. like this:
-(MBNHomeScreenButton*) initWithImage:(UIImage*)image
andHighlightedImage:(UIImage*)highlightedImage
andTitle:(NSString*)aTitle
withSelector:(SEL)actionButton
forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 100, 120);
self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
self.imageView.center = self.center;
self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.highlighted = NO;
[self addSubview:self.imageView];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
self.title.text = aTitle;
self.title.textAlignment = UITextAlignmentCenter;
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
// This creates a transparent button over the view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.backgroundColor = [UIColor redColor];
[button setFrame:self.frame];
[button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
[self addSubview: button];
}
return self;}
In the ViewController I create the View like this:
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
andTitle:#"News"
withSelector:#selector(newsButtonPressed)
forDelegate:self];
m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
Still... the newsButtonPressed doesn't get called.
Can you help?
Thanks!
====================
[edit]
Ok... figured it out.. Thanks guys.
It was the fact that I was trying to add the view as a subview to a UIImageView with the last line:
[backgroundImgView addSubview:m_newsHSButton];
Apparently, it won't get touches anymore.
Thanks for the effort guys!
Try making the button a #property or instance variable in your custom view. Then call [m_newsHSButton.button addTarget:self action:actionButton forControlEvents:UIControlEventTouchUpInside]; from the viewController.
I'm trying to add a textfield into a navigation bar, but it doesn't show up in the simulator. I do it as follows inside viewdidload:
UITextView *textField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 170, 44)];
self.navigationItem.titleView = textField;
Any ideas?
Thanks!
i have done it in this way. i know that you have got to the answer uptil now
but still this will helpful to someone else so
this can be done as follows
-(void)viewWillAppear:(BOOL)animated
{
imgVw =[[UIImageView alloc]initWithFrame:CGRectMake(68, 7, 180, 28)];
[imgVw setImage:[UIImage imageNamed:#"textfieldBackground.png"]];
[self.navigationController.navigationBar addSubview:imgVw];
textfieldTxt = [[UITextField alloc]initWithFrame:CGRectMake(78, 11, 170, 25)];
textfieldTxt.backgroundColor = [UIColor clearColor];
[textfieldTxt setAutocorrectionType:UITextAutocorrectionTypeNo];
textfieldTxt.delegate = self;
[self.navigationController.navigationBar addSubview:textfieldTxt];
}
please don't forgot to remove from superview, when viewdidunload
- (void)viewDidUnload
{
[super viewDidUnload];
[textfieldTxt removeFromSuperview];
[imgVw removeFromSuperview];
}
thanks