make non-move UITextField on background of UITableView - objective-c

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;

Related

White line on the bottom on ios 7 with UITextField

I have text field, i need to show the cursor and hide the keyboard. It looks ok in the IOS 6 and IOS 5. But in IOS 7 i can see small white line on the bottom.
My code is
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[scrollview setDelegate:self];
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
[scrollview setBackgroundColor:[UIColor blackColor]];
scrollview.contentSize = CGSizeMake(768, 1024);
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 500, 100)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.returnKeyType = UIReturnKeyDone;
[textField setUserInteractionEnabled:YES];
[textField setEnabled:YES];
[scrollview addSubview:textField];
// Here I need to show the cursor without showing keyboard
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
((UITextField*)textField).inputView = dummyView;
[((UITextField*)textField) becomeFirstResponder];
Anyone having any thought how to fix this?
I think this is due to the keyboard view, whose having the height as 1.
So if you change the line
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
to
UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
it will work.

ScrollView is not working when try to add as a subview

I have problems with my scrollView! I have a view called Rootview and I wanna add three subviews to a scrollview. When I add the scrollview as a subview the middle view and the three subviews disappear. This is my code.. The Rootview (whole window) and the scrollview are outlets.
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 218, 2024, 218)];
[middleView setBackgroundColor: [UIColor yellowColor]];
[RootView addSubview:middleView];
//create new views for sap systems beginning from left
UIView *firstSystem1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 218)];
[firstSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:firstSystem1];
UIView *secondSystem1 = [[UIView alloc] initWithFrame:CGRectMake(305, 0, 300, 218)];
[secondSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:secondSystem1];
UIView *thirdSystem1 = [[UIView alloc] initWithFrame:CGRectMake(610, 0, 300, 218)];
[thirdSystem1 setBackgroundColor: [UIColor grayColor]];
[middleView addSubview:thirdSystem1];
CGSize scrollViewContentSize1 = CGSizeMake(2024, 218);
[middleScrollView setContentSize:scrollViewContentSize1];
[middleScrollView addSubview:middleView];
Can anyone help me with that?
The problem is that you aren't adding your middleScrollView anywhere
change this
[RootView addSubview:middleView];
for this
[RootView addSubview:middleScrollView];

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];

Using subviews in scrollerview iPhone

I am trying to add uipickerview in uiscrollview, but it is not being shown properly, just a black square.
Further, I have to add 1 more uipickerview, some labels and some buttons.
The code I am using is as under:
-(void)loadView {
[super loadView];
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.png"]];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroller.pagingEnabled = YES;
NSInteger numberOfViews = 2;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.height;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
// awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroller addSubview:awesomeView];
[scroller addSubview:pView];
}
scroller.contentSize = CGSizeMake(self.view.frame.size.width , self.view.frame.size.height * numberOfViews);
[self.view addSubview:scroller];
}
what can be the issue?
Are you putting data in the picker view? I mean, if you just put the picker view without data on it you are not going to see anything on it.
You need to add the delegate and the datasource and put some data in your picker!
pView.delegate = self;
pView.datasource = self;
and then you have to implemente at least:
numberOfComponentsInPickerView:
numberOfRowsInComponent:
titleForRow:
make sure you are implementing uipickerviewdelegate methods. its normal that it will show just the black square because your pickerview cant find the data and thus does not display anything.
UIPickerView *pView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 20, 280, 165)];
Change height to 216 .. UIPickerView has fixed height of 216 ..only.. you can't change it..

how to implement scrollview for dynamically created textfields and labels

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];