UISearchBar cancel button disabled when keyboard disappears on iPad - ios

I have my UISearchBar placed in a navigation bar. When I tap on the bar, it expands itself and shows the cancel button. If you type something in the field and then you tap on "Search" on the keyboard, it dismiss the keyboard showing you the results. But in this moment I'm having a big problem : the cancel button is now disabled (greyed out) and if I tap on it, first the keyboard is opened and then I can tap again on the Cancel to exit from the search. Is it possible to keep the Cancel button active always so I can directly tap on it to exit from the search?

searchBar.showsCancelButton = YES;
You can create another custom button on UISearchBar.
for (UIView *subView in searchBar.subviews) {
if([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton *)[searchBar.subviews lastObject];
//Make changes to the cancelButton here
[cancelButton setEnabled:YES];
}
}

Related

Hide keyboard on button press IOS

I need to hide the IOS keyboard when i press a button on the screen.
Whatever i try, the keyboard doesn't go away when i press the button on the screen.
// This is the button
-(IBAction)showDateView:(id)sender
// hide keyboard
[self.view endEditing:YES];
}
Thanks
[self.textfield resignFirstResponder];
- resignFirstResponder
Notifies the receiver that it has been asked to relinquish its status as first responder in its window.
in general there may be more than one textfields in your screen you don't know which textField must be resigned so add all the textField objects into an array and iterate a loop to resignFirstResponder
for (uilabel *textField in labelObjArray) {
[textField/textView resignFirstResponder]
}
the key board will be resigned immediate
[self.view endEditing:YES];
This will work if the responder was initiated from the view.
[self.navigationController.navigationBar endEditing:YES]
This will work if the responder was initiated from the navigation bar.

Dismiss Keyboard

So I have a self created top bar controller that is being implemented in my other controllers views. I have a textfield on this top bar. I was wondering what the best approach to having the keyboard dismiss if the user clicks anywere outside the keyboard. I do have a tap gesture recognizer that performs the method dismisskeyboard. However, this only works if the user clicks on the top bar outside the keyboard. Is there a way to set it up so if the user clicks anywere on the screen, then this will dismiss the keyboard?
The approach I would describe is a hack but still works.
create a transparent UIButton with the frame of the view, like below:
UIButton* overlay = [UIButton buttonWithType:UIButtonTypeCustom];
overlay.frame = self.view.bounds;
overlay.backgroundColor = [UIColor clearColor];
[overlay addTarget:self action:#selector(hideOverlay:) forControlEvents:UIControlEventTouchDown];
[self.view.subviews[0] insertSubview:overlay belowSubview:self.textField];
Create a method hideOverlay to dismiss the keyboard and hide the transparent:
-(void)hideOverlay:(id)sender {
UIView* overlay = sender;
[overlay removeFromSuperview];
[self.textField resignFirstResponder];
}
You should ideally call the first block of code in textFieldDidBeginEditing: protocol method of UITextFieldDelegate and you should register your calling class accordingly.
You might try giving the text field a transparent inputAccessoryView, sized to fill the rest of the screen, that catches taps and dismisses the keyboard.

Adding UISearchBar with Scope button as subview to UIView

I want to add UISearchbar with two scoop button to my UIView. i do not want to either navigation or table view.
when user open model view page from sheet comes up with Search option and rest. when user click on Search button scoop button to be visible.
i tried to do but i am not getting desire result.
how to remove Background of scope button, i have removed uisearchbar, similar way i do not want white background for button. is it possible. showsScopeBar i am making FALSE still background can be visible.
To remove Background image of scope buttons just try :
Class segmentedControlBackgroundView = NSClassFromString(#"_UISegmentedControlBackgroundView");
for(UIView *view in self.searchBar.subviews) {
if([view isKindOfClass:[UISegmentedControl class]]){
for(UIView *subview in view.subviews) {
if ([subview isKindOfClass:segmentedControlBackgroundView]) {
subview.hidden = YES;
}
}
}
}

Return on no text in UISearchbar

I have a UISearchbar that has the return key on the keyboard that is greyed out unless I type text into the searchbar. How do I allow it to appear on no text? If thats not possible, how do I return to my view if theres no text in the searchbar?
Heres some code:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
touchtoCancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-211)];
[touchtoCancel addTarget:self
action:#selector(DidPressCancelSearch:)
forControlEvents:UIControlEventTouchUpInside];
[self.search setFrame:CGRectMake(self.search.frame.origin.x, self.search.frame.origin.y-210,
self.search.frame.size.width, self.search.frame.size.height)];
return YES;
}
-(IBAction)DidPressCancelSearch:(id)sender
{
[search resignFirstResponder];
}
One method besides the built-in cancel button that was already mentioned... You can create an invisible button that takes up the area above the keyboard which enables only when editing the search bar. Tapping on this invisible button (space between search bar and top of the keyboard) should resign the first responder.

Force keyboard to show up via button (iOS)

I have an UITextView and I don't want check editable option, how can call keyboard via a button?
This code doesn't work for me!
-(IBAction) yourButtonClick
{
[myTextView becomeFirstResponder];
[self.view addSubview:myTextView];
}
From the iPhone Application Programming Guide
However, you can programmatically
display the keyboard for an editable
text view by calling that view’s
becomeFirstResponder method. Calling
this method makes the target view the
first responder and begins the editing
process just as if the user had tapped
on the view.
So to show the keyboard programmatically,
[textView becomeFirstResponder];
However, the keyboard will never show if the textView is not editable.
The purpose of showing the keyboard is to allow editing. I assume you just don't want the keyboard to appear when the user taps the text view. In this case, you can enable editable programmatically when the button is tapped.
-(IBAction) yourButtonClick
{
myText.editable = YES;
[myText becomeFirstResponder];
}
Then in the UITextViewDelegate, disable editable when the user finishes editing.
- (void)textViewDidEndEditing:(UITextView *)textView {
textView.editable = NO;
}

Resources