I'm simply trying to use a UITextField for my iMessage App.
The problem is with when it is in compact mode (MSMessagesAppPresentationStyleCompact) once you select the textfield, all the views disappear. It seems to work fine in expanded mode.
What is the proper way of using a textfield in compact mode? Thanks
Solved
It appears that you can only use textfields while in expanded mode, so you'll need to implement something like this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if([self presentationStyle] == MSMessagesAppPresentationStyleCompact) {
[self requestPresentationStyle:MSMessagesAppPresentationStyleExpanded];
self.didRequestKeyboard = YES;
return NO;
}
return YES;
}
-(void)didTransitionToPresentationStyle:(MSMessagesAppPresentationStyle)presentationStyle {
// Called after the extension transitions to a new presentation style.
// Use this method to finalize any behaviors associated with the change in presentation style.
if(presentationStyle == MSMessagesAppPresentationStyleExpanded){
if(self.didRequestKeyboard){
[self.textField becomeFirstResponder];
self.didRequestKeyboard = NO;
}
}
}
I struggled with this problem (still present as of iOS 10.2) and ended on this workaround:
fileprivate class TextField: UITextField {
override var canBecomeFirstResponder: Bool {
if let viewController = viewController as? MSMessagesAppViewController,
viewController.presentationStyle == .compact {
viewController.requestPresentationStyle(.expanded)
return false
}
return super.canBecomeFirstResponder
}
}
I say "workaround" because I feel this is a problem Apple should solve ergo this solution should be temporary. This implementation, as an alternative to the original answer's, is segregated and can be easily ripped out.
Same solution but using blocks
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if presentationStyle != .expanded {
didTransitionHandler = {
textField.becomeFirstResponder()
self.didTransitionHandler = nil
}
requestPresentationStyle(.expanded)
return false
}
return true
}

