UITextField Not Allowing Input - objective-c

I am programatically adding a UITextField into a UIImageView. When I set the text property, I can see the text.
The issue is, it's not letting me input text into it like an input field when I run the app.
This is my code for initializing the TextField:
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(38.0f, 0.0f, self.view.frame.size.width - 38.0f, 40.0f);
textField.text = #"Hello";
textField.background = [[UIImage imageNamed: #"field.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:19];
textField.backgroundColor = [UIColor whiteColor];
textField.userInteractionEnabled = YES;
[bar addSubview: textField];
Do I need to do something special to allow for text input?

If bar is an UIImageView, try to set
bar.userInteractionEnabled = TRUE;

Text Field not added into imageview . You can add textfield on view,for user interaction enabled.
First you can add imageview and then add text field on the view.
Like This--
[self.view addSubview:imgV];
[self.view addSubview: textField];

Related

How to get a certain subview from UIView by tag

I'm a noob in Objective-C and I have one question.
I have one UILabel object that I adding to one UIView with this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,10,self.view.frame.size.width-15-70, 30)];
label.tag = 1;
label.font = [PublicObject fontTexts:17];
label.textAlignment = NSTextAlignmentRight;
label.textColor = [UIColor whiteColor];
[cell setBackgroundColor:[UIColor clearColor]];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
view.backgroundColor = [PublicObject colorWithHexString:#"cd4110"];
label.text = [filterData objectAtIndex:indexPath.row];
view addSubview:label];
Now I want get one subview in my view where this subview has tag = 1 and save it on another object like this:
UILabel *tagLabel;
tagLabel = // I want get one subview in view where tag = 1
Please help me understand how to do this.
Example with UILabel:
UILabel *label = (UILabel *)[self.view viewWithTag:1];
good luck!
You can get your subviews with for loop iteration
for (UIView *i in self.view.subviews){
if([i isKindOfClass:[UILabel class]]){
UILabel *newLbl = (UILabel *)i;
if(newLbl.tag == 1){
/// Write your code
}
}
}
Swift
let label:UILabel = self.view.viewWithTag(1) as! UILabel
You can get the subview with the code which others have mentioned, just like
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
But an important point to remember,
Make sure the parent view doesn't have the same tag value as of the subview. Otherwise the viewWithTag: method will return the receiver view (on which you are calling viewWithTag:) instead of returning the actual subview you want.
So keep the parent view and child views tags distinct whenever you need to use viewWithTag:.
Swift 3, Swift 4 and Swift 5
if let subLabel: UILabel = primaryView.viewWithTag(123) as? UILabel {
subLabel.text = "do things here :-D"
}
You could use the viewWithTag: method.
If you are on the same view
UILabel *tagLabel = (UILabel*)[view viewWithTag:1];
Also, if you want a new instance of UILabel
UILabel *newTagLabel = [tagLabel copy];
//customize new label here...
[view addSubView:newTagLabel];

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

UINavigationBar setTitleTextAttributes

I'm trying to change the color of NavigationBars title while I'm on a viewcontroller (not before pushing it). by using:
[[[self navigationController] navigationBar] setTitleTextAttributes:textAttributes];
But this line of code only works before pushes or pops. I was wondering if there is a way to force this without navigating?
I would say the simplest way is to create an UILabel with the same style you want for the UINavigationController title and set the label to the navigationItem.titleView.
Try this one
UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.text = self.navigationItem.title;
self.navigationItem.titleView = label;
create custom label and set it as titleView of your navigation bar

Adding list to a view without changing views

How can I add a list (TableView) to a view without changing views, I want to show the list over the current view but in the middle of the view, so the original view will be showed also.
I tried with label...but gettign some problems adding tableView instead of label.
UIView *testView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 100, 100)autorelease];
testView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2f]; UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = #"TEST";
[label sizeToFit];
[testView addSubview:label];
[self addSubview:testView ];
Refer mjpopupviewcontroller link as it is according to your requirement

get a subview back after placing it into the view

so in my app, i click a button and i dynamically add a UITextField:
myRect = CGRectMake(4, viewYPos, 80, 25);
UITextField *myTextField = [[UITextField alloc] initWithFrame:myRect];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.font = [UIFont fontWithName:#"Trebuchet MS" size:12];
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
[myTextField addTarget:self
action:#selector(textFieldShouldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[attributeScrollView addSubview:myTextField];
[myTextField release];
now, how do i reference this text field? is there a way to pull it out of the view and place it back into a new textfield variable?
my only thought is to create an instance nsarray variable so i can keep their reference.
Make myTextField an instance variable instead of local. Then, don't release it.