Show Clear Button of UITextField even it is empty - ios

I have a UITextField for search in my Application.
When user taps on the Clear Button in the right of the Search Text Field I need to hide this textfield.
So, can I show the Clear Button then TextField is empty? I need to do it always to provide the ability hiding TextField.

Just create a Custom button and set it as rightView for your TextField
UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// Set clear image for your button
[cleanBtn setImage:img forState:UIControlStateNormal];
[cleanBtn addTarget:self action:#selector(cleanTextField:) forControlEvents:UIControlEventTouchUpInside];
yourTextField.rightViewMode = UITextFieldViewModeAlways;
yourTextField.rightView = cleanBtn;

I tried to fix this issue with creating a custom button and setting the rightView of my text field. It did not solve my problem. There is no button on the right view.
I have added UIButton in Interface Builder using Auto Layout and placed it on my text field. It works. It's strange solution but it works.

Related

How to set title text of button outside viewDidLoad

How can I set the title text of a button without using inside viewDidLoad? I know how to set the title text of a button:
[button setTitle:#"Title" forState:UIControlStateNormal];
But I want to set the title text outside viewDidLoad.
Please create an IBOutlet of UIButton set title in the xib/storyboard

putting button in navigationItem.titleView programmatically

I wanted to put a timer in the title label of a tableViewController. To my surprise, storyboard let me drop a button the title and I got everything working, however, I need to button to differentiate between single and double taps. Single tap starts and stops the timer, and double tap would reset it back to zero. In order to make a button recognize both type of taps, I had to create a subclass of UIButton, which means that I am now adding the button programmatically rather than through storyboard. In viewDidLoad, I tried to add the button to the titleView of the navigationItem but nothing's showing when I run it in the simulator. I set the x,y coordinates like this
self.navigationItem.titleView.bounds.size.width/2
thinking it would place the button in the middle.
I'm not sure if I've messed up the coordinates or done something else wrong, but nothing's showing. Can you suggest how it might be done?
MMTimerButton *button = [MMTimerButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(self.navigationItem.titleView.bounds.size.width/2, self.navigationItem.titleView.bounds.size.height/2, 160.00, 40.0);
[self.navigationItem.titleView addSubview:button];

programmat button and storyboard button

I have one button and one search field in navigation bar and i added a new button by code from one class for all view.
The problem is: with the button view the other button and the search field added with storyboard are not more touchable and not working.
UIImage * buttonImage = [UIImage imageNamed:#"chat-notify.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(goChat)
forControlEvents:UIControlEventTouchDown];
//[button setTitle:#"1" forState:UIControlStateNormal];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:button];
and then in the controller:
notificationViewController* notification = [[notificationViewController alloc]init];
//[notification methodA];
[notification makeButtonNotification];
[self.view addSubview:notification.view];
Does anyone know where is my mistake?
Thanks
Why are you adding the button surely having it there and changing it's hidden state would be easier then you can keep it all in the storyboard
UIControlEventTouchUpInside
is what you should use not "UIControlEventTouchDown"
TouchUpInside is usually the event where you trigger the action from a button. It's when the user is lifting up from the button after pressing it. You don't want to trigger the action on Touch Down as the use may move his finger off the button to cancel the press, which is standard and expected behaviour on iOS
UPDATE: Base on your comment to this answer perhaps you need to add the button to a specific point in your view heir achy. As in your storyboard views will be added as they are listed in that stroyboard starting from the top down, with the one on the bottom being the top view.
Try obviously making sure that views that you want to receive touches are not directly on top of each and that they do not overlap. Also try adding the programatically generated button above or below a specific view, rather than just making it the top subview in your main view which by the sounds of it isn't what you want.
As you're adding a button like this
[self.view addSubview:button];
it may be better to do it more like this
[self.view insertSubview:button aboveSubview:someOtherView]
To do this you'll probably need to hook up your "someOtherView" to an IBOutlet so you can reference it in your code. As it looks like you're most likely sticking your button over everything else, when you need to be more precise.

Set image of uiButton in custom table cell

I have a uitbutton within a custom table cell. I am trying to set the buttons image with
[cell.thumbImage setImage:[UIImage imageNamed: #"full_breakfast.jpg"] forState:UIControlStateNormal];
It is displaying a blue button instead of the image like so:
Am I doing this wrong? When i remove the uibutton setImage it returns to the normal button. This SHOULD be fairly simple so sorry if it is.
In iOS7 there is new button type called UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
Check your .xib file and change button type to Custom, or programatically you can do this:
[UIButton buttonWithType:UIButtonTypeSystem];

Clear button on UITextView

How can I add a clear button (cross inside a circle) for UITextView like UITextField has?
Based on the answer from GhostRider a more accurate and up to date implementation:
int kClearButtonWidth = 15;
int kClearButtonHeight = kClearButtonWidth;
//add the clear button
self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.clearButton setImage:[UIImage imageNamed:#"UITextFieldClearButton.png"] forState:UIControlStateNormal];
[self.clearButton setImage:[UIImage imageNamed:#"UITextFieldClearButtonPressed.png"] forState:UIControlStateHighlighted];
self.clearButton.frame = CGRectMake(0, 0, kClearButtonWidth, kClearButtonHeight);
self.clearButton.center = CGPointMake(self.textView.frame.size.width - kClearButtonWidth , kClearButtonHeight);
[self.clearButton addTarget:self action:#selector(clearTextView:) forControlEvents:UIControlEventTouchUpInside];
[self.textView addSubview:self.clearButton];
And the method
- (void)clearTextView:(id)sender{
self.textView.text = #"";
}
You can use this images for the two states of the button:
just make a uibutton and put it on uitextview and set its action for clear text view;
uitextview.frame = (0,0,320,416);
uibutton.frame = (310,0,10,10);
[uibutton setimage:#"cross.png" forcontrolstate:uicontrolstatenoraml];
[uibutton addTarget:self action:#selector(clearButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
-(void)clearButtonSelected{
uitextview=#"";
}
hope you want to clear the text view text when you click on cross button above is help
if not understand then i can send you proper program for that
From product perspective, if you're going to have a clear button, you probably want to use a UITextField instead of a UITextView and UITextField supports a clear button natively - set the clearButtonMode property as such:
UITextField *textfield = ...;
textfield.clearButtonMode = UITextFieldViewModeAlways;
See screenshot:
You could use UITextFieldViewModeWhileEditing to only present the clear button while the user is actively updating the content.
There's nothing built in like there is for the UITextField. You'd have to add the view yourself (probably a UIButton) and place it correctly and also somehow get the text to wrap around it correctly. (And I don't think the latter is really possible.)
Maybe instead you should display a toolbar above the keyboard (or an inputAccessoryView if you're targeting 3.2 and later) that provides a clear button.
For me changing the .frame or the .contentInset properties did not work.
For me the best result came from:
1) adding a UIView to the controller, give it round corners and a border to mimic a UITextView.
self.viewTextBackground.layer.borderColor = [UIColor colorWithRed:171/255.0 green:171/255.0 blue:171/255.0 alpha:1.0].CGColor;
self.viewTextBackground.layer.borderWidth = 1.0f;
self.viewTextBackground.layer.cornerRadius = 9.0f;
2) place UITextView on top of this UIView. Place it so that the borders of the underlying UIView stay visible.
3) give the UITextView round corners:
self.textNote.layer.cornerRadius = 9.0f;
3) Make its width fe. 30pixels less compared to the underlying UIView. You now have space for a clear-button.
4) simply add a UIButton to your controller and place it in the top-right corner of the underlying UIView.
5) change the buttons properties: set its type to 'custom' and set its image to the image of a grey cross.
6) bind an action to the button te clear the UITextView
You can add a clear button like the one in the attached screenshot with minimal coding. Just follow these steps:
Select the storyboard and drag a UIButton into your UITextView
Set the buttons constraints
Assign a title or a background image
Create the button's IBOutlet reference and the action (see onClearPressed(_:) below) for "Touch Up Inside" in the ViewController
Implement the textViewDidChange(_:) UITextViewDelegate delegate method, and make sure to set the button's isEnabled property based on the textfield content, e.g.:
func textViewDidChange(_ textView: UITextView) {
clearButton.isEnabled = !textView.text.isEmpty
}
Implement the onClearPressed(_:) action:
#IBAction func onClearPressed(_ sender: Any) {
textView.text = ""
clearButton.isEnabled = false
}
That's it.

Resources