I have a chat app with UITableView
and in the bubbles inside the UITabelViewcells
I present a UILabel
that contains the content of the message.
When the user long press the UIImageView
(declared inside the cell class) of the bubble - I present a UIMenuController
.
[self.bubbleImageView addGestureRecognizer:recognizer];
In addition to the tableView
I have a UITextView
for the user to write new messages.
If the focus is on the textView
(keyboard shown) and then the user long-press a bubble the keyboard hides and the user experience is bad...
How do i fix this issue and keep the keyboard on the screen even if the textView
is no longer the first responder ?
of course i don't want the keyboard to present itself if it wasn't already on the screen.
p.s I tried to understand this answer- UIMenuController hides the keyboard but didn't really figure out what what is self.messageTextView is...
@implementation TextChatCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[recognizer setMinimumPressDuration:0.5];
[self.bubbleImageView addGestureRecognizer:recognizer];
}
-(BOOL)canBecomeFirstResponder{
return YES;
}
- (BOOL) becomeFirstResponder {
return [super becomeFirstResponder];
}
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(copy:))
return YES;
if (action == @selector(delete:)) {
return YES;
}
return [super canPerformAction:action withSender:sender];
}
- (void)copy:(id)sender {
[[UIPasteboard generalPasteboard] setString:self.contentLabel.text];
[self resignFirstResponder];
}
-(void)delete:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:DELETE_MESSAGE object:self.chatMessage userInfo:nil];
[self resignFirstResponder];
}
- (void) handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {
if (longPressRecognizer.state != UIGestureRecognizerStateBegan)
return;
if ([self becomeFirstResponder] == NO)
return;
UIMenuController *menu = [UIMenuController sharedMenuController];
// Create Rect
CGRect targetRect =self.bubbleImageView.frame;
[menu setTargetRect:targetRect inView:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(menuWillShow:)
name:UIMenuControllerWillShowMenuNotification
object:nil];
[menu setMenuVisible:YES animated:YES];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
if ([self isFirstResponder] == NO)
return;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
[menu update];
[self resignFirstResponder];
}
- (void) menuWillHide:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillHideMenuNotification object:nil];
self.bubbleImageView.alpha = 1.0;
}
- (void) menuWillShow:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(menuWillHide:)
name:UIMenuControllerWillHideMenuNotification
object:nil];
self.bubbleImageView.alpha = 0.5;
}