I'm trying to have some text in a UITextView change color (and sometimes change text) after a button press:
I have loaded a text view in ViewController with a simple string in viewDidLoad
self.answer1TextView.text = questions[1];
The above displays just fine.
There is button next to it, which triggers the following method (also in ViewController):
- (IBAction)select1ButtonTapped:(id)sender {
self.answer1TextView.textColor = [UIColor redColor];
}
The problem is this: when the button is tapped, the text in the UITextView doesn't change color, and the right half of the view disappears.
I'd greatly appreciate some advice on this. Thank you.
Related
I am making an app where you have to enter your name. Then when you press the go button, the text field, button, and text vanish. But I don't really know how to.
if you just need to vanish you can make it hidden
yourButton.hidden = YES;
or
yourButton.alpha = 0.0;
or you can remove it like
[yourButton removeFromSuperview];
Put all UIButton, UITextView, UITextField, UIImageView in one view and let that view get hidden when you click Go button. This way you don't have multiple hidden statement for each UIButton, UITextView, UITextField, UIImageView, etc.
So final statement would be
yourView.hidden = YES;
In storyboard I have a UITableViewController-->UITableView-->UITableViewSecion--> with static cells
in the same UITableView I also have a UIView that holds a background image and UITextView.
A click on a button shows the UIView and set it's frame, which appears OK, but as soon as I click on the UITextView or make it firstResponder programmatically the keyboard appears and the view disappears
the code when clicking the button
self.myView.frame = CGRectMake(0, self.tableView.contentOffset.y+100, self.tableView.frame.size.width, self.tableView.frame.size.height-100);
self.myView.hidden = NO;
how can I fix this?
Can you copy paste us the code where you add the view to the tableview? Are you doing it with constraints or with frames?
The issue you are having is probably due to the fact the UITableViewControllers automatically shrink the contentSize of the UITableView they hold when the keyboard shows. If you add a UIView to your tableView with addSubview: programmatically, you might need to add a flexible bottom resize mask to make sure when the contentSize shrinks in height, your view stays attached to the top and not the bottom.
Try this on viewDidLoad:
[theViewYouAddedToTableView setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
I've got a program with slide out menu. There's Menu button(BarButtonItem) in the right position. When the view controller loads I'm doing next
_menuBarButton.target = self.revealViewController;
_menuBarButton.action = #selector(revealToggle:);
So, when I click on the this button the View Controller slides out to right and I see another View Controller.
So, I want to make the main view controller blur when it slides out. I've got a code how to do it blur, but I can't implement this code because when I tap on the bat button it runs revealToggle: selector.
I've tried:
1. To set action for bar button. And firstly blur view controller:
- (IBAction)menuBarButtonTapped:(UIBarButtonItem *)sender {
[self setBlurEffect];
_menuBarButton.target = self.revealViewController;
_menuBarButton.action = #selector(revealToggle:);
[self.menuBarButton.target performSelector:#selector(revealToggle:)];
}
But app crashes with "unrecognized selector" (the solutions of this problem doesn
t help too).
I've wanted to use willDisapear method but it doesn't run, because the main view controller doesn't disapear. It just slides out.
So, could you help me with this problem?
P.S. I'll be happy if you propose any other effects for main view controller except blur.
P.P.S. Sorry for many mistakes in question.
I've found a good answer for me. I don't use several ViewControllers to implement menu panel.
Now I'm using another UIView calls menuPanel. It contains tableView(UITableView) and blurView(UIView). The second one is under first one.
You can blur this view in to ways:
From the code using next method.
(void) setBlurEffect {// Add blur view
CGRect boundsView = self.someView.bounds;
UIVisualEffectView *tableViewVisualEffect = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
self.tableViewVisualEffect.frame = boundsView;
self.tableViewVisualEffect.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableViewVisualEffect.backgroundColor = [UIColor clearColor];
[UIView transitionWithView:self.view
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations: ^ {
[self.someView addSubview:self.tableViewVisualEffect];
}
completion: nil ];
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in above code to add effects to custom view.
}
Where someView is view you want to do blur(or any other effect)
From the story board with Visual Effect View.(I've chosen)
After all I've set tableView and blurView into menuPanelView as pinned to all sides of the menuPanel with 0 distance.
And the last thing I've done I'm changing the position of the menuPanelView with animation:
[self.menuPanelView setFrame:self.visibleTableViewFramePosition];
[UIView animateWithDuration:0.8
animations:^{
[self.menuPanelView setFrame:self.invisibleTableViewFramePosition];
} completion:nil];
Where invisibleTableViewFramePosition is CGRect variable contains position of the menuPanelView when it has to be invisible. You also need to create visibleTableViewFramePosition.
That's all. Hope it may be helpful for someone=)
The UITextFields in my app have placeholder text defined (in Interface Builder), and I cannot cause these fields to acquire focus (i.e. show the keyboard and allow editing) when I tap on the area occupied by the placeholder text. If I tap on the textfields in an area just outside the that of placeholder text (though still within the bounds of the textfiled itself), it acts as normal (i.e. the keyboard pops up and I can edit the content of the textfield). How can I fix this?
Thanks.
EDIT 1
Ok, I think I've got it. I'm also setting a blank view to the "leftView" property of these UITextFields. If I remove this, you can touch the UITextFields in the area of the placeholder text and it reacts as expected; I need this view for the leftView though. If you change the background color of this spacer view to red, you can see that it doesn't get in the way at all, so I don't know what's going wrong.
Why does this code cause this problem?
Thanks.
+(UIView*)getTextFieldLeftSpacerViewWithBackgroundColor:(UIColor*)backgroundColor andHeight:(CGFloat)height
{
UIView *leftWrapper = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 8.0f, height)];
leftWrapper.autoresizingMask = UIViewAutoresizingNone;
[leftWrapper setOpaque:YES];
if(backgroundColor){leftWrapper.backgroundColor = backgroundColor;}
else{leftWrapper.backgroundColor = [UIColor clearColor];}
return [leftWrapper autorelease];
}
+(void)setTextFieldLeftSpacerForTextFieled:(UITextField*)textField
{
if(textField)
{
UIView *spacer = [MYViewController getTextFieldLeftSpacerViewWithBackgroundColor:nil andHeight:textField.bounds.size.height];
textField.leftView = spacer;
textField.leftViewMode = UITextFieldViewModeAlways;
}
}
Just ran into the same problem and didn't want to subclass, just had to use :
leftWrapper.userInteractionEnabled = NO;
I abandoned this approach. Instead of using an invisible view to offset the text, I opted to subclass UITextField and provide offset CGRects for the bounds of the text within theUITextField. The following SO post was very helpful:
Indent the text in a UITextField
I have a popover that contains a UITableView. This UITableView has a cell with a text field in it:
alt text http://cl.ly/1b50a21ca8202d22db1b/content
When the popover opens near the bottom of the screen, and I tap the text field to edit it, the keyboard comes up, and the popover moves up to avoid being covered by the keyboard. But as it moves up, the table view in the popover scrolls up out of bounds:
alt text http://cl.ly/4fe64fbfe9518f20560d/content
I can scroll it back down, but how do I prevent this from happening.
I've come to the conclusion that this is a bug. I have filed a bug report with Apple (rdar://8156616) as well as a report on OpenRadar.
For anyone interested, here is a sample project that demonstrates the issue.
Try disabling scrolling on the table view.
[self.tableView scrollingEnabled:NO];
How are you setting the content view of your pop over controller. Please try editing the auto-resizing mask of the content view and set it from top-left.
Hope this helps.
Thanks,
Madhup
Perhaps you can set the UITableView's contentSize manually? So that there is no excess for it to scroll up.
I had the same problem but when hiding the keyboard and reloading the table view!
There is one solution for this problem! What you have to do is first hide the keyboard and reload the table view or change the table view in method recieving keyboard did hide notification!
At first I called
[textView resignFirstResponder]; or [textField resignFirstResponder];
and then
-(void)keyboardDidHide:(NSNotification *)notif {
//Check some conditions if you want
[tableView reloadData];
}
You need to listen to the keyboard will show notification:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardStatusForPopover(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
Then you have to see if the keyboard frame intersects with the popover and subtract the values added from the keyboard to the popover container frame:
#objc func handleKeyboardStatusForPopover(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if keyboardSize.intersects((self.resultPopOverTableViewController!.view.superview!.superview!.frame).insetBy(dx: 0, dy: 60)) {
let popOverContainer = viewControllerDisplayedbyPopover?.view.superview!.superview!
// cellDisplayingDetailInfo = sourceview of popover
popOverContainer!.frame.origin.y = popOverContainer!.frame.minY - keyboardSize.height + cellDisplayingDetailInfo.frame.maxY
}
}
}