I am new to ios. I have the following code to change the return key type of keyboard to join. It works fine in ios6 but not in ios7. The code inside the if-block is never executed on ios7.
Have anyone seen a similar issue? Is there any workaround for this?
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[searchBar resignFirstResponder];
[(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyJoin];
[searchBar becomeFirstResponder];
}
I googled and found that Join button isn't showing up because the internal structure of UISearchBar, which aren't supposed to modify, has changed. I dont konw how to fix this and where to found out what has changed, Anyone can explain ?
Also tried following(similar way) workaround code not lucky enough
for(UIView *searchBarSubview in [searchBar subviews]) {
if([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)searchBarSubview setReturnKeyType: UIReturnKeyJoin];
} else {
for(UIView *subSubView in [searchBarSubview subviews]) {
if([subSubView conformsToProtocol:#protocol(UITextInputTraits)]) {
[(UITextField *)subSubView setReturnKeyType: UIReturnKeyJoin];
}
}
}
Try this :
UITextField *searchBarTextField ;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ?
self.searchBar.subviews : [[self.searchBar.subviews
objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.returnKeyType = UIReturnKeyJoin;
try this for get textfield from searchbar
for (UIView *subView in self.searchBar.subviews){
for (UIView *searchView in subView.subviews){
if ([searchView isKindOfClass:[UITextField class]])
{
[(UITextField *)searchView setKeyboardAppearance:UIKeyboardAppearanceAlert];
[(UITextField *)searchView setEnablesReturnKeyAutomatically:NO];
break;
}
}
}
Related
I Tried using some sample code but i Can't ..Please sombody Help With the solution...
The code which i tryed is
for (UIView *subview in [controller.view subviews]) {
for (UIView *myview in [subview subviews]) {
for (UIView *theview in [myview subviews]) {
if ([NSStringFromClass([theview class]) isEqualToString:#"MFMailComposeView"]) {
for (UIView *aview in [theview subviews]) {
for (UIView *thisview in [aview subviews]) {
if ([NSStringFromClass([thisview class]) isEqualToString:#"MFComposeScrollView"]) {
for (UIView *bview in [thisview subviews]) {
if ([NSStringFromClass([bview class]) isEqualToString:#"MFComposeTextContentView"]) {
NSLog(#"%#", ((UITextView *)bview).text);
}
}
}
}
}
}
}
}
}
But Its not going inside the loop in this line...
if ([NSStringFromClass([theview class]) isEqualToString:#"MFMailComposeView"]) {
I searched in many sites...what i got is ,this code is broken in ios 6... BUT I am using ios 8.1....Please help me come up with the solution...
Thanks In Advance
Nivetha
Through some fantastic help from a very patient user I managed to get my reset button working with the following code:
- (IBAction)resetAction:(id)sender {
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.text = #"";
}
}
The issue I am having now is that this will not reset the textfields in a scroll view. Any help will be gratefully received! I am so close to completing this!!!!
Many Thanks
You could call the method recursivly:
- (void)resetTextfieldsInView:(UIView*)view {
for (UIView *v in [view subviews]) {
if ([v isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)v;
textField.text = #"";
}else if ([v isKindOfClass:[UITextView class]]) {
UITextView *textView = (UITextView *)v;
textView.text = #"";
}else{
[self resetTextfieldsInView:v];
}
}
}
- (IBAction)resetAction:(id)sender {
[self resetTextfieldsInView:self.view];
}
If the textfields are embedded in a scroll view, then you need to iterate the subviews of the scroll view, i.e.
for (UIView *view in [self.scrollView subviews])
Please see my below code :
I have a searchbar in my viewController
for (UIView *subView in self.searchBar.subviews)
{
if([subView conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subView setDelegate:self];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDefault];
[(UITextField *)subView setBackground:[UIImage imageNamed:#"shopping-Search"]];
}
}
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
This code is working fine in iOS 6 but not working in iOS 7.
The searchBar sub views hierarchy has been changed in iOS7, try the below:
iOS7:
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
iOS6 and before:
NSArray *searchBarSubViews = self.searchBar.subviews;
Try below updated function:
for (UIView *subView in searchBarSubViews) {
if([subView conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subView setDelegate:self];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDefault];
[(UITextField *)subView setBackground:[UIImage imageNamed:#"shopping-Search"]];
}
}
for (UIView *subview in searchBarSubViews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
In iOS6 , I use the below code to change inputView of UISearchbar
for (UIView *view in _searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
UISearchbar in iOS7 had changed, I don't know how to find textField to change inputView.
Please help! Thanks!
The hierarchy of subview has been changed in iOS7, so you can use the following code:
// subviews
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField* search=(UITextField*)view;
[search setFont:[UIFont fontWithName:#"MyCustomFont" size:15]];
search.delegate = self;
[search setInputView:self.customKeyboard];
[self.customKeyboard setTextView:search];
}
}
[self.searchBar reloadInputViews];
Use following code :
NSArray *subViewsOfSearchBar = [[self.YOurSearchBar.subviews objectAtIndex:0] subviews];
for(int i =0; i< subViewsOfSearchBar.count; i++) {
if([[subViewsOfSearchBar objectAtIndex:i] isKindOfClass:[UITextField class]])
{
UITextField *searchTxtField=(UITextField*)[subViewsOfSearchBar objectAtIndex:i];
[searchTxtField setInputView:self.customKeyboard];
}
}
[self.searchBar reloadInputViews];
Try this code :
for (UIView *view in _searchBar.inputView.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField* textField = (UITextField*)view;
[textField setInputView:_myKeyboard];
break;
}
}
in iOS 7 subview is not working. From iOS7 UI objects have a extra wrapper/container. May be this code will help you.
I want to be able to search for a String without any character in it. The problem is, that the default keyboard does not show up the search button, when there is nothing written in the UISearchBar.
Any idea?
Thanks,
Markus
Luckily, I just happened to be working on code that does exactly this. It's a bit of a hack, but you can find the UITextField that is embedded within the UISearchBar, then turn off enablesReturnKeyAutomatically:
UITextField *searchBarTextField = nil;
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
Daniels answer works perfect till iOS 7.
iOS 7 Update
- (void)searchBarTextDidBeginEditing:(UISearchBar *) bar
{
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f) ? bar.subviews : [[bar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}
Type * and use that as a placeholder in your program for "" (nothing).
Searching for "" is like searching for nil / NULL: the query will return all entries that are empty or null.
The * is a wildcard character used in most searching markups, and it'll return all matches.
while searching a text. it is difficult to search a string without character...
so you need to work on
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
//write code here
}
it will run automatically
this is what i did
To remove keyboard upon backspace till blank:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText isEqualToString:#""]) {
[searchBar resignFirstResponder];
}
}
you will also need to set your uitextfield within your uisearchbar to the same delegate , remember to add to this delegate (in my code's context the delegate is self)
for (UIView *view in searchBar.subviews){
if ([view isKindOfClass: [UITextField class]]) {
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
following that add these to your delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *) aSearchBar {
[aSearchBar resignFirstResponder];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:textField.superview afterDelay: 0.1];
return YES;
}
when any of these trigger , perform your search for "" string