Marin Marin - 1 month ago 9
iOS Question

Instagram cancel button

I am using a

webView
to show Instagram login. However there is no cancel button. How do I add a cancel button? I tried to add a cancel button on top of the webView but it's not relative to the webView, in that if you were to drag down that window as if to refresh it,the cancel button will remain static. Is it possible to have it right next to the
Log in
button? If not is the
uINavigationBar
the best way?

Answer

If all you want from the cancel button is to close the webView, you can have the webView embedded in a UIViewController which is added to the UINavigationController stack. Then you will be able to add the Cancel Button as a UIBarButtonItem on the navigation bar.

For example:

Your home screen is MainViewController and you tap a button to show Instagram in webView. Then in your MainViewController.m (which is already the rootViewController of the UINavigationController) do this:

// Init your web view controller and push to UINavigationController stack to show it.
WebViewController *yourWebViewContainer = [self.storyboard instantiateViewControllerWithIdentifier:@"YourControllerIdentifier"];
[self.navigationController pushViewController:yourWebViewContainer animated:YES];

Then in your WebViewController.m file, create the cancel button in viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTapped:)];
}