I want to change UITextField inside UISearchBar by subclass of UISearchBar. But when edit in UISearchDisplayController's UISearchBar, the field doesn't change the size which i set in :layoutSubView method.
How should I do that?
UISearchBar *searchBar = yourSearchController.searchBar;
UITextField *searchField;
for(int i = 0; i < [searchBar.subviews count]; i++) {
if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if(!(searchField == nil))
searchField.frame = CGRectMake(4, 5, 290, 30);
Good Luck.
Related
I have a UITextField for the UISearchBar which this was working until iOS 7 upgrade and now it fails at this line: UITextField *textfield=(UITextField*)[[searchBar subviews] objectAtIndex:1];
any idea how to fix this? thanks
// search bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 190.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(90.0, 0.0, 230.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
searchBar.layer.borderColor=[UIColor whiteColor].CGColor;
UITextField *textfield=(UITextField*)[[searchBar subviews] objectAtIndex:1];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
try this , it's work in Both IOS6 and IOS7+ and safe approch
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"ArialMT" size:10]];
It's not a good idea to assume that second searchBar's subview will be UITextField.
I printed out subviews for UISearchBar, that's what I got on iOS 7:
<__NSArrayM 0x17d141f0>(
<UIView: 0x17d34f40; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x17d34fa0>>
)
Only one subview, so your ... objectAtIndex:1] will definitely crash.
You can use the following category for UIView to find UITextField in your searchBar:
#interface UIView(Utils)
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass;
#end
#implementation UIView(Utils)
-(UIView*)findSubviewRecursivelyOfClass:(Class)subviewClass
{
if( [self isKindOfClass:subviewClass] ) {
return self;
} else {
for( UIView* child in self.subviews ) {
UIView* result = [child findSubviewRecursivelyOfClass:subviewClass];
if( result ) {
return result;
}
}
return nil;
}
}
#end
Try this one for iOS7.
TESTED
for (id object in [searchBar subviews])
{
for (id subObject in [object subviews])
{
if ([subObject isKindOfClass:[UITextField class]])
{
UITextField *textfield=(UITextField*)subObject;
}
}
}
iOS6
for (id object in [searchBar subviews]) {
if ([object isKindOfClass:[UITextField class]]) {
UITextField *textfield=(UITextField*)object;
}
}
How about a recursive method that can trick to work in any version
UITextField *searchBarTextField = [self findTextFieldFromControl:self.placeSearchBar];
- (UITextField *) findTextFieldFromControl:(UIView *) view
{
for (UIView *subview in view.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
return (UITextField *)subview;
}
else if ([subview.subviews count] > 0)
{
return [self findTextFieldFromControl:subview];
}
}
return nil;
}
Hi I have a method in which I have created 3 views on a click;
-(void)method{
for (i=0; i<3; i++) {
NSLog(#"i is: %d",i);
NSLog(#"i is: %d",i);
userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake (100,41,60,60)];
userResizableView.tag = i;
imageVw1 = [[UIImageView alloc]initWithFrame:userResizableView.bounds];
imageVw1.image = [UIImage imageNamed:#"redacted2.jpg"];
imageVw1.userInteractionEnabled=YES;
imageVw1.autoresizesSubviews = YES;
imageVw1.alpha = 0.93; // for opacity
userResizableView.contentView = imageVw1;
userResizableView.delegate = self;
[userResizableView showEditingHandles];
currentlyEditingView = userResizableView;
lastEditedView = userResizableView;
[self.view addSubview: userResizableView];
[userResizableView release];
}
}
Now in another method I want to hide these views what I created last.
but I am not able to do it. I am hiding my view is like-
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
userResizableView.tag = a;
userResizableView.hidden = YES;
a++;
}
But only one view is hide and the rest are remaining, no matters how amny times I clicked Hide_method, only one view is hide.
My question is that how to hide last view what I created last. Means views hide like 3,2,1,0. On every time when I clicked hide_method.
Any Idea or suggestions would be highly welcome.
Provide unique tag to not clash with any other subview of self.view with same tag like say any number 999 +.
Make changes accordingly:
for (i=0; i<3; i++) {
SPUserResizableView *userResizableView = [[SPUserResizableView alloc] initWithFrame:CGRectMake(100,41,60,60)];
userResizableView.tag = 999+i;
..........
..........
}
Now hide method would be:
-(void)Hide_method{
// find SPUserResizableView with unique tag
for (i=0; i<3; i++) {
if(i == 2){
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+i];
userResizableView.hidden = YES;
}
}
EDIT : To restore frame to original store while allocating view
CGRect prevFrame = userResizableView.frame //take frame while allocating userResizableView
Now restore whenever you wanted to original frame like this:
SPUserResizableView*userResizableView = (SPUserResizableView *)[self.view viewWithTag:999+2]; //get last userResizableView
userResizableView.frame = prevFrame;
-(void)Hide_method{
// int type instance variable, and a=0; in viewdidload;
//userResizableView.tag = a;
SPUserResizableView *_view = (SPUserResizableView *)[self.view viewWithTag:a];
userResizableView.hidden = YES;
a++;
}
Try this:
-(void)Hide_method{
//UIView *v = [self.view viewWithTag:a];
SPUserResizableView *v = (SPUserResizableView *)[self.view viewWithTag:a];
if(v)
[v removeFromSuperview];
a++;
}
Method to remove view having tag=a
for (UIView *subview in [self.view subviews])
{
if (subview.tag == a)
{
[subview removeFromSuperview];
}
}
I'm having some trouble dynamically removing UITextFields from my scrollview when the user taps a button. The UITextields were created programatically. This is what I have thus far, any help would be appreciated.
-(IBAction)resetAll{
int textFieldTag;
for (int i=0; i<[array count]; i++) {
textFieldTag = i + 100;
UITextField *myTextField = (UITextField *)[self.view viewWithTag:textFieldTag];
[myTextField removeFromSuperview];
[myTextField release];
}
}
-(IBAction)resetAll
{
NSMutableArray *arrayTextFields=[yourScrollView subViews]; //get all subviews from your scrollview
for (int i=0; i<[arrayTextFields count]; i++)
{
if([[arrayTextFields objectAtIndex:i] isKindOfClass:[UITextField class]]) //check for UITextField
{
UITextField *textField=(UITextField *)[arrayTextFields objectAtIndex:i];
[textField removeFromSuperView]; //Remove textField
}
}
}
Implement this....
-(IBAction)resetAll
{
for (UITextField *myTextField in [myScrollView subviews])
[myTextField removeFromSuperview];
}
Try this:
-(IBAction)resetAll
{
for (UITextField *tf in myScrollView) {
[tf removeFromSuperview];
[tf release];
}
}
I have many textfields created in my application, and i want them to close keyboard when user finishes editing. I know that if textField's created by IB, and there is an IBAction i can use but what if the textField's created like this,
int top = 60;
for(NSUInteger i=0; i< [kArray count]; i++){
UITextField *kField = [kArray objectAtIndex:i];
kField.frame = CGRectMake(130, top, 60, 30);
kField.borderStyle = UITextBorderStyleRoundedRect;
kField.autocorrectionType = UITextAutocorrectionTypeNo;
kField.returnKeyType = UIReturnKeyDone;
kField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[scrollView addSubview:kField];
top += 50;
}
How can i close the keyboard when user presses done button?
Use UITextFieldDelegate method:
#pragma mark UITextFieldDelegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
Remember to make your class comply with delegate like so:
#interface MyViewController : UIViewController <UITextFieldDelegate>
Try this:
- (BOOL)textFieldShouldReturn: (UITextField *)textField {
[kField resignFirstResponder];
return YES;
}
In my application I have to add a search bar at the head of the tableview. I am able to add the searchbar but problem is without adding default search bar of ios can i add my customize search bar?? I am giving an image to see what types of search bar will be there...
you can subclass the UISearchBar and override the layoutSubviews method :
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:#"yourImage.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
Also you can :
//to clear searchbar backgraound
- (void) clearSearchBarBg
{
for (UIView *subview in theSearchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
}
//display showSearchButtonInitially in a keyboard
- (void)showSearchButtonInitially
{
UIView * subview;
NSArray * subviews = [theSearchBar subviews];
for(subview in subviews)
{
if( [subview isKindOfClass:[UITextField class]] )
{
NSLog(#"setEnablesReturnKeyAutomatically");
[((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
((UITextField*)subview).delegate=self;
[((UITextField*)subview) setEnabled:TRUE];
((UITextField*)subview).borderStyle = UITextBorderStyleNone;
break;
}
}
}
Look for Apple DOC for UISearchBar
You have bunch of methods there to get whatever you want
You can get UITextView Inside the search bar by
UITextField *textField = [searchBar.subviews objectAtIndex:2];
if ([textField isKindOfClass:[UITextField class]]) {
//Do your customization
}
Again look for AppleDoc for UITextField. You have bunch of methods for that also.
Yeah definitely. You can make your custom search bar (which is a sub-class of UIView) and add it as subview to the tableHeaderView.
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];
here searchBarDesign is my searchBar name.
I think it's better just set all properties of UISearchBar when it is loaded.
#interface MySearchBar : UISearchBar
#end
#implementation MySearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self myInitialize];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self myInitialize];
}
-(void)myInitialize
{
self.backgroundImage = [UIImage imageNamed:#"image.png"];
for (UIView* subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
//customize text field
UITextField* textfield = (UITextField*) subview;
}
}
}
#end