user339946
user339946

Reputation: 6119

iMessage App - Using a UITextField in compact presentation style

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

Upvotes: 3

Views: 532

Answers (3)

AlKozin
AlKozin

Reputation: 916

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
}

Upvotes: 0

Nathan Hosselton
Nathan Hosselton

Reputation: 1335

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.

Upvotes: 1

user339946
user339946

Reputation: 6119

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;
        }
    }
}

Upvotes: 4

Related Questions