I created a search bar programmatically and added to my view using the codes below:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xPositionForSearchBar, yPositionForSearchBar, widthForSearchBar, heightForSearchBar)];
UIView *bg = [[searchBar subviews] objectAtIndex:0];
searchBar.delegate = self;
searchBar.placeholder = #"Search record";
for(UIView *view in searchBar.subviews){
if([view isKindOfClass:[UITextField class]]){
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
[bg removeFromSuperview];
[self.view addSubview: searchBar];
}
The code is implemented with UISearchBarDelegate and UITextFieldDelegate.
I have tried using
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
NSLog(#"cancel clicked");
searchBar.text = #"";
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(#"clear");
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:searchBar afterDelay: 0.1];
return YES;
}
and yet, the text inside the searchBar is not cleared at all when i click on the "clear button" - a circle with a "X" inside.
The clear button works when I implemented it in IB. Wonder why?
Kindly advice, many thanks.
This might happen if you position your search bar out of the bounds of a parent view. Then, the touches aren't delivered to your searchBar properly. This also affects text editing controls like copy & paste.
I have checked your code it work fine on device as well as on simulator.
Related
I have a UITextView that is created programmatically on viewDidLoad using the method below. Keyboard is shown successfully right after [_answerTextView becomeFirstResponder] is called. And hid if the user enters return(\n) character.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[self endEditing:YES];
return NO;
}
return YES;
}
After the keyboard is hid by the code above, I'm not able to bring it back anymore by tapping the UITextView again.
I tried showing the keyboard by calling [_answerTextView becomeFirstResponder] on Tap event for the parent UIView and it worked. But that's not what I want.
UITextView is created by the method below;
-(UITextView*)createSurveyTextView{
_answerTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, _optionsStackView.frame.size.width, 200)];
_answerTextView.keyboardType = UIKeyboardTypeDefault;
_answerTextView.text = #"";
_answerTextView.editable = true;
_answerTextView.selectable = true;
_answerTextView.delegate = self;
[_answerTextView becomeFirstResponder];
_answerTextView.backgroundColor = [UIColor whiteColor];
_answerTextView.textColor = [UIColor blackColor];
_answerTextView.layer.cornerRadius = 5;
[_optionsStackView addSubview:_answerTextView];
_answerTextView.font = [UIFont systemFontOfSize:20];
[_answerTextView setReturnKeyType:UIReturnKeyDone];
[_optionsStackView bringSubviewToFront:_answerTextView];
[self bringSubviewToFront:_answerTextView];
[_answerTextView setUserInteractionEnabled:true];
UITapGestureRecognizer *mainViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTextViewTap:)];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap2:)];
[_answerTextView addGestureRecognizer:mainViewTap];
[self addGestureRecognizer:tap2];
return _answerTextView;
}
I want the UITextView to simply show the keyboard when the user taps on it self. What am I missing?
Edit: I have the same sources in another view, but everything works fine there. The only difference is, here the UITextView is created programmatically.
Edit 2: I got it worked. The _optionsStackView that the UITextView is added as a child is a UIStackView. I tried adding the _answerTextView to the parent UIView and everything works fine now. What would be the problem with the UIStackView?
Your problem very strange in your code.You can explantion why use UITapGestureRecognizer to this UITextView control.How do you think this lead to conflict?
I find your problem where in start,but I create new project to ensure.So I confirm that UITextView already has target with itself.Also you can't to add UITapGestureRecognizer.
What you're doing is almost correct, but just try changing endEditing: to [textView resignFirstResponder]:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
If that doesn't work, then it could be that one of your gestures is preventing the UITextView's own UITapGestureRecognizer from receiving the tap. Try disabling (or comment out the creation of) both of your recognizers to verify that it is the issue. If it is, then you can fix it using the UIGestureRecognizerDelegate methods.
You did not use common way to hide/close the keyboard.
Try this common way:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return [textField resignFirstResponder];
}
This is a method in the UITextFieldDelegate, use it and remove your code:
if([text isEqualToString:#"\n"]) {
[self endEditing:YES];
return NO;
}
Edit:
#siburb You are right. My bad.
UITapGestureRecognizer maybe the root cause.
Edit:
#Semih Akbas
Repalce:
[_optionsStackView addSubview:_answerTextView];
To:
[_optionsStackView addArrangedSubview:_answerTextView];
To show a help view I've put an UIImageView which behind has some UIButtons. How can I disable user interaction of these buttons? If I touch this image where buttons are behind, they responds to touch events.
CODE FOR IMAGE BACKGROUND:
self.helpBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.helpBackground.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
self.helpBackground.hidden = YES;
[self.view addSubview:self.helpBackground];
I've used self.helpBackground.userInteractionEnabled = NO; but didn't work.
Thanks.
Put your buttons in a array and loop through them and disable them:
NSArray *buttonsArray = #[yourButton1, yourButton2];
for (UIButton *button in buttonsArray) {
button.enabled = NO;
}
And when you want them enabled just loop again and set enabled to YES
Keep a Tag to your helpView(imageview) and add the following code
for (UIView *view in self.view.subviews) {
if (view.tag != yourViewTag) {
view.userInteractionEnabled = NO;
}
}
And after removing the help screen use the following code
for (UIView *view in self.view.subviews) {
view.userInteractionEnabled = YES;
}
you can try below solution..When help imageview is appearing
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = NO;
}
else
{
view.userInteractionEnabled = YES;
}
}
////After dismissing the help screen you can
for (UIView *view in [self.view subviews])
{
if (view isKindOfClass:[UIButton Class])
{
view.userInteractionEnabled = YES;
}
}
////(OR) Simply do as below
self.view.userInteractionEnabled=YES;
Hope it helps you..
You can create a method that disables user interaction for all views that are under your helpBackground:
- (void)disableUserInteractionForViewsUnderView:(UIView *)view
{
CGRect area = view.frame;
for (UIView *underView in [self.view subviews]) {
if(CGRectContainsRect(area, underView.frame))
underView.userInteractionEnabled = NO;
}
}
and after that you call it where you need it:
[self disableUserInteractionForViewsUnderView:self.helpBackground];
EDIT
I've created a UIViewController category gist on github.com. You can find it here: https://gist.github.com/alexcristea/0244b50e503e8bf4f25d
You can use it like this:
[self enumerateViewsPlacedUnderView:self.helpBackground usingBlock:^(UIView *view) {
if ([view isKindOfClass:[UIButton class]]) {
view.userInteractionEnabled = NO;
}
}];
I have been attempting to hide a UISearchBar keyboard but my code fails to run successfully. I have the following code which removes the cursor but the keyboard continues to show:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder]; // using method search bar
[theSearchBar resignFirstResponder]; // using actual object name
[self.view endEditing:YES];
}
I have set the delegate in the .h as <UISearchBarDelegate> and set searchBar.delegate = self; in .m
Any ideas? Thanks.
Try to use following methods for search an resign first responder:
- (UIView *)findFirstResponderBeneathView:(UIView *)view {
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:#selector(isFirstResponder)] && [childView isFirstResponder] ) return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result ) return result;
}
return nil;
}
- (void)hideKeyboard {
[[self findFirstResponderBeneathView:self.view] resignFirstResponder];
}
When i press the back button. the iCarousel is still shows up for 1 second.why is this happening and how to stop this.I have used storyboard to create a iCarosel view..
- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}
- (void)dealloc
{
carousel.delegate = nil;
carousel.dataSource = nil;
}
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [idOfAllWords count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250.0f, 250.0f)];
((UIImageView *)view).image = [UIImage imageNamed:#"page.png"];
view.contentMode = UIViewContentModeCenter;
label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:50];
label.tag = 1;
[view addSubview:label];
}
else
{
label = (UILabel *)[view viewWithTag:1];
}
Words *word=nil;
word=idOfAllWords[index];
label.text =word.Name;
return view;
}
Hiding and unhiding is not the solution. What you need is just one line:
yourCarousel.clipsToBounds = YES;
I actually tried to reproduce your problem and did see that the carousel view stays for a second when 'pop' or back button press happens. This particularly happens when you the carousel is swiped and then the back button pressed. As a workaround, I was able to fix it by setting the iCarousel hidden in the viewWillDisappear method.
- (void)viewWillDisappear:(BOOL)animated
{
[YOUR_CAROUSEL_NAME setHidden:YES]; //This sets the carousel to be hidden when you press Back button
}
If this looks to be hidden suddenly, you can perhaps try setting the alpha to 0.0 inside an animation block. Something like this:
- (void)viewWillDisappear:(BOOL)animated
{
//[YOUR_CAROUSEL_NAME setHidden:YES];
[UIView animateWithDuration:0.2f animations:^{
[YOUR_CAROUSEL_NAME setAlpha:0.0f]; //This makes the carousel hide smoothly
}];
}
Hope this helps!
I am working on IOS 7 application.By default its appearing like Pic(1).But I need to change it as Pic(2).I googled and found few answers for the requirement,but it has not changed.Or else I need to hide.So that I can manage with background image.This is first image
I used below code to modify it.But didnt succeed.
In .h file
#property(nonatomic,strong) IBOutlet UISearchBar *findSearchBar;
In .m file
#synthesize findSearchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
[self setSearchIconToFavicon];
}
- (void)setSearchIconToFavicon
{
// The text within a UISearchView is a UITextField that is a subview of that UISearchView.
UITextField *searchField;
for (UIView *subview in self.findSearchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
break;
}
}
if (searchField)
{
UIView *searchIcon = searchField.leftView;
if ([searchIcon isKindOfClass:[UIImageView class]])
{
NSLog(#"aye");
}
searchField.rightView = nil;
searchField.leftView = nil;
searchField.leftViewMode = UITextFieldViewModeNever;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
}
I am not getting how to make the center of the view's image to nil.Its really killing my time.Please help me.where I had gone wrong.
UITextField *txfSearchField = [looksearchbar valueForKey:#"_searchField"];
[txfSearchField setBackgroundColor:[UIColor whiteColor]];
[txfSearchField setLeftViewMode:UITextFieldViewModeNever];
[txfSearchField setRightViewMode:UITextFieldViewModeNever];
[txfSearchField setBackground:[UIImage imageNamed:#"searchbar_bgImg.png"]];
[txfSearchField setBorderStyle:UITextBorderStyleNone];
//txfSearchField.layer.borderWidth = 8.0f;
//txfSearchField.layer.cornerRadius = 10.0f;
txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
txfSearchField.clearButtonMode=UITextFieldViewModeNever;
Try this may be it will help u........
I'm not sure how to left-align the placeholder, but as of iOS 5.0 there's a simple, supported way to modify the search bar's text field properties, e.g.:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setLeftViewMode:UITextFieldViewModeNever];
which will hide the magnifying glass icon.
You could try:
searchBar.setImage(UIImage(named: "yourimage")!, forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)