how to implement scrollview for dynamically created textfields and labels - objective-c

I am creating some 6 to 10 textfields dynamically and also some labels dynamically.I want to implement scroll view for this.how to do it?i have dragged a scroll view and implemented below code in the ViewDidLoad method.Even i connected IBOutlet for scrollview.But nothing is working.
Can anyone tel me with sample where i can add all this textfields into a scrollview and when it exceeds scroll view limit it should be scrolled down to see the textfields.
- (void)viewDidLoad
{
[self textField1];
[self textField2];
[super viewDidLoad];
scrollView.frame = CGRectMake(0, 0, 600, 800);
[scrollView setContentSize:CGSizeMake(500, 800)];
}
// code for creating textfield dynamically using -(void)textfield function
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 250, 200, 35)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 300, 200, 35)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 350, 200, 35)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 400, 200, 35)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 450, 200, 35)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 500, 200, 35)];

In your Main View, add one Scroll View. After that inside the Scroll View add UIView as Subview.
Like This: MainView -->
ScrollView --->
SubView
Then in ViewDidLoad:
scrollView.contentSize = CGSizeMake(320, 500);
scrollView.frame = CGRectMake(0.0,0.0,320.0, 480.0);
subView.frame = CGRectMake(0.0,0.0,320.0, 460.0);
After that add TextFields Dynamically inside the SubView, not in MainView.

Get my code from here. Im sure it will help

After creating the textfields, add textfields to the scrollview.
[scrollview addSubview:textfield];

Related

make non-move UITextField on background of UITableView

How to make UITextField on background of UITableView?
I want to set textField in the top and when i scroll tableView my textField don't move.
Something like this
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 100, 30)];
self.textField.backgroundColor = [UIColor redColor];
self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:NSStringFromClass([TableViewCell class])];
[self.view addSubview:self.textField];
}
You have another solution - besides one suggested in comments:
You can create UIViewController with tableView and text field. Assign delegate and dataSource to the view controller. Pin text field to the top and you have your desired behaviour. You will have to connect tableView with IBOutlets.
If you want to set textField in the top change textField frame
From
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 100, 30)];
To
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
self.textField.backgroundColor = [UIColor redColor];
[self.view addSubview:self.textField];
Also another solution is add textField to navigation item title.I tried this and it works fine.
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
textField.backgroundColor = [UIColor redColor];
[self.view addSubview:textField];
self.navigationItem.titleView = self.textField;

Unable to set rightview or leftview in textfield of searchbar in ios 7

Purpose:
I want to set right view as label in searchbar's textfield
Following Code working fine in ios 6 for doing the same thing:
UISearchBar *search = [[UISearchBar alloc] init];
[search setTintColor:[UIColor grayColor]];
search.frame = CGRectMake(0, 150, 320,50);
search.delegate = self;
UITextField *searchField=[search.subviews objectAtIndex:1];//Changed this line in ios 7
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
[self addSubView:search];
When i have Run the same code in ios 7(with xcode 7.0.2 fully updated), It was giving me error, after googling i got that hiearchy of searchbar to textfield has been changed, so i have modified my code as:
replaced commented line in above code with:
UITextField *searchField=[((UIView *)[search.subviews objectAtIndex:0]).subviews lastObject];
after doing this, it is not giving me any error(also checked the logs by printing the description of textfield and everything is working good), when i run it, it gives textfield with red background but the rightView's label is not shown to me. Now, can anyone help me how to show this rightview.
If you want to do this programatically then you have to declare UISearchBar object in your .h file.
UISearchBar *searchBar;
And in your .m file you can code like:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
searchBar = [[UISearchBar alloc] init];
[searchBar setTintColor:[UIColor grayColor]];
searchBar.frame = CGRectMake(0, 150, 320,50);
searchBar.delegate = self;
[self.view addSubview:searchBar];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField;
if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0)
searchField=[searchBar.subviews objectAtIndex:1];
else
searchField=[((UIView *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
I think this will work for both iOS7 and prior versions of iOS7.
First of all, your approach is not good and not maintainable across iOS versions.
I think that initialization of UISearchBarTextField's right view is taking place after you set your custom view to it's rightView property. You will see correct result if you set searchField.rightView in viewDidAppear method:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField=[((UIView *)[_searchBar.subviews objectAtIndex:0]).subviews lastObject];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
I've tested it on iOS 7 simulator and it works.
You have to get the text field first.
No need to index subviews to get text field or bar button.
You can directly get the text field and bar button
In swift
let textField = searchBar.valueForKey("searchField") as? UITextField
let cancelBarButton = searchBar.valueForKey("cancelBarButtonItem") as? UIBarButtonItem
In Objective-C
UITextField *textField = [searchBar valueForKey:#"searchField"]:
UIBarButtonItem *cancelBarButton = [searchBar valueForKey:#"cancelBarButtonItem"]:
Then you can set the left view and right view with
textField.leftView = yourView
textField.rightView = yourView

textfield for ios

i am very new to iOS and xcode. i want to create a textfield without using nib files or storyboard. i searched for the answers but everywhere its kind of confusing or they have used nib files. i have created buttons and its working fine but for textfields m facing problems. here is my viewcontroller.m please help.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextField *textfield;
button.frame = CGRectMake(10 ,350 , 300, 40);
button1.frame = CGRectMake(10 ,50 , 300, 40);
textfield.frame = CGRectMake(10, 100, 200, 30);
[button setTitle:#"Change screen to green!" forState:UIControlStateNormal];
[button1 setTitle:#"click screen to red!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self action:#selector(buttonPressed1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:textfield];
}
-(void)buttonPressed {
self.view.backgroundColor = [UIColor greenColor];
}
-(void)buttonPressed1 {
self.view.backgroundColor = [UIColor redColor];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
thanks & regards
You just declared the UITextField instead of allocating. You must allocate it first then set frame & add it on the view like -
UITextField *txt = [[UITextField alloc] init];
txt.frame = CGRectMake(10, 100, 200, 30);
txt.delegate = self;
[self.view adsubview:txt];
It solves your problem.
First use
UITextField *textfield = [[UITextField alloc] init];
instead of
UITextField *textfield;
Then set backgroud color of textfield since the backgroud color will be clearColor by default.
textfield.backgroundColor = [UIColor whiteColor];
or use
textfield.borderStyle = UITextBorderStyleRoundedRect;
to get white colored bordered text field.
If you've added the alloc init like the answers have suggested, your text field is in fact on the screen, but since it has no border or no text, you can't see it. Try adding this line, and then you will see it:
textfield.borderStyle = UITextBorderStyleRoundedRect;
Use this:
UITextField *textfield = [[UITextField alloc] init];
Insetd of
UITextField *textfield;
UITextField *textfield; Just crete object reference Not object.
UITextField *textfield =[[UITextField alloc] init];
textfield.frame = CGRectMake(10, 100, 200, 30);
textfield.delegate = self; [self.view addSubview:textfield];
[self.view bringSubViewToFront:textfield];
may solve the problem

Disable interaction on upper view

I have two views in my program. One is statusView, it's alpha is set to 0.8f. And below it is tableView. How do I disable statusView such way, that clicking on it would not click tableView. Settings userInteractionEnabled = false; didn't help.
Here is how i add views.
[self.view addSubview:_builded.view]; // obj with my tableView
info = [[Info alloc] init];
[self.view addSubview:info.view]; // statusView
If you add a subview like the following, the tableView underneath would not catch the user interaction.
UITableView *testTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
testTableView.delegate = self;
testTableView.dataSource = self;
[self.view addSubview:testTableView];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
statusView.backgroundColor = [UIColor redColor];
statusView.alpha = 0.8;
[self.view addSubview:statusView];

On view hierarchy, clarification needed

In this code example, i am trying to produce the following view hierarchy
Window -> background image -> scroll view -> text view
All i see however is
Window -> background image
What am i missing please?
-(void) viewWillAppear:(BOOL)animated {
UIScrollView *scrollWindow = [[UIScrollView alloc]
initWithFrame:CGRectMake(30, 30, 440, 212)];
UITextView *scrollableText = [[UITextView alloc] init];
[scrollableText setEditable:NO];
[scrollableText setText:#"Why, hello there"];
[scrollWindow addSubview:scrollableText];
UIImage *backgroundImage = [[UIImage alloc] initWithCGImage:
[UIImage imageNamed:#"about_bg.png"].CGImage];
UIImageView *backgroundView = [[UIImageView alloc]
initWithImage:backgroundImage];
[backgroundView addSubview:scrollWindow];
[[self view] addSubview:backgroundView];
}
Andrew is right about not making the scroll view a subview of the background UIImageView view. But the scroll view is invisible. Only its content (scrollableText) will show. And you haven't set scrollableText's frame, so it's effectively invisible too. Init like so:
[scrollableText setEditable:NO];
[scrollableText setText:#"Why, hello there"];
[scrollableText setFrame:CGRectMake(0, 0, 100, 100)];
And you should see it.
What you need is this hierarchy:
Window
background image view
scroll view
text view
Try adding two subviews to your window, not shove them all as subviews into each other.''-
- (void) viewWillAppear:(BOOL)animated {
UIScrollView *scrollWindow = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 30, 440, 212)];
UITextView *scrollableText = [[UITextView alloc] init];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"about_bg.png"]];
scrollableText.editable = NO;
scrollableText.text = #"Why, hello there";
[scrollWindow addSubview:scrollableText];
[self.view addSubview:backgroundView];
[self.view addSubview:scrollWindow];
}