One of the most popular articles on Touch Code Magazine is the one demoing my MTPopupWindow class. It was a class I put quickly together to showcase how you can create a cool animation and kind of – animate in a popup window on the screen. Much to my surprise a lot of comments came in and people started using the class and also started sending me questions and demands for more functionality. (Yes I reply all the mail coming from Touch Code Magazine – despite whether I can help the person or not)
With the release of iOS6 I thought that MTPopupWindow is a perfect showcase and a great opportunity to use the new Auto-Layout feature of iOS. Coincidentally this is also the most requested feature for the class – being able to handle both Portrait and Landscape orientations.
So, yes – there’s a new – version 2.0 of the class and it’s ready for download (at the bottom of this article)
NB: The new version of MTPopupWindow is only iOS6 compatible, because it uses the latest features of iOS.
But before we come to the download link, let me showcase what you can do with the new class.
How to show a popup window
First step – you will need to add the QuartzCore.framework to your Xcode project:
- Click on the Xcode project file
- Make sure your active target is selected
- Click on the “Build Phases” tab
- Click the (+) button and select QuartzCore.framework from the list
Done!
Now in your Objective-C source file add at the top an include to the MTPopupWindow class, like this:
#import "MTPopupWindow.h" |
And there you go! Just as before you can add an HTML file to your Xcode project and show it in a modal window with one line:
[MTPopupWindow showWindowWithHTMLFile:@"info.html" insideView:self.view] |
In fact the method name remains the same, so you can just “upgrade” the version of MTPopupWindow without changing the code of your app. What this method does is to load the file “info.html” and show the modal window in the view, which you pass. (the example code is supposed to run in a view controller – so self.view usually points to the current view on the screen)
You still get the same neat animation as with version one:


Featuring Auto-Layout
Unlike version one – the current version is fully Auto-layout capable. Meaning the popup window has the proper constraints defined, so when you rotate the device you will still get a nice maximized window:
The MTPopupWindow draws all graphics by using QuartzCore and therefore does not use any fixed size image files – Portrait, Landscape, Retina or non-Retina, it’s all the same. The window is drawn perfectly.
Also the window just grows in height to fill in the iPhone5 4inch display nicely:
Custom sized windows
Many people requested a popup window, which fits in different sizes of view controllers. Many of the readers wanted to use the popup window in a tab controller, navigation controller or their own custom view controllers.
That is now as well possible – you just tell the popup which is its parent view and it takes care itself to maximize itself into that view:
//full screen window [MTPopupWindow showWindowWithHTMLFile:@"info.html"]; //inside the current view controller (can be tab controller as on the image above) [MTPopupWindow showWindowWithHTMLFile:@"info.html" insideView:self.view]; |
Full control over the object
You have now the possibility to use your own workflow instead of the predefined one-shot methods. You can go about creating the view in the usual manner, like so:
MTPopupWindow* winPop = [[MTPopupWindow alloc] init]; winPop.fileName = @"info.html"; [winPop showInView:self.view]; |
Advanced HTML content
In the new version of MTPopupWindow you can use images and links even in your local HTML files!
It’s really easy, if you want to have an image in your HTML file, just add the image to your XCode project and then load it using an IMG tag.
And then just use normal HTML in your file:
<img src="bundle_books_cover_noprice.jpg"> |
And voila, the image pops up in your popup window:

But that’s not all! You can also link to other HTML files in your XCode project! How cool is that? Very
Just use normal HTML links and make your popup window content interactive, like so:
<a href="more.html">More testimonials</a> |
When the user taps the link above the file more.html will be loaded in your popup. Awesome!
Full control of the components
Since MTPopupWindow inherits directly UIView you can tweak all properties you like to. Also MTPopupWindow has one extra property called webView and it’s the web view of the popup window – you can directly access and control it as well. Sweet!
Here’s for example how to change the background of the popup window if you have a yellow background of your HTML file:
MTPopupWindow* winPop = [MTPopupWindow showWindowWithHTMLFile:@"info.html" insideView:self.view]; winPop.backgroundColor = [UIColor yellowColor]; |
Loading web content from Internet
And finally what better way to have an up to date company information inside your app, but loading it straight from the Internet? Instead of HTML file name you can just pass in a web site URL and the popup window will show it:
[MTPopupWindow showWindowWithHTMLFile:@"http://www.ios5-by-tutorials.com" insideView:self.view]; |
Web pages scale to fit by default.
Download link
This time I decided to upload the code to GitHub as it seems it becomes more and more popular for this kind of code sharing. So head up and download the demo project, which also includes the MTPopupWindow class here:
https://github.com/icanzilb/MTPopupWindow
I hope this article was useful, download class code and use it as will. You can include it in commercial apps, attribution is appreciated, but not required.
Where to go from here?
Of course give the MTPopupWindow a try in one of your projects.
Also if you want to learn more about the iOS6 technology that makes MTPopupWindow so flexible – Auto-Layout – you can read on it in detail in the two chapters dedicated to Auto-Layout in “iOS6 by Tutorials.”
Cheers, Marin
The post was originally published on the following URL: http://www.touch-code-magazine.com/showing-a-popup-window-in-ios6-modernized-tutorial-code-download/
·











Thanks for the helper class! It works good except I can’t seem to get localization of the info.html file working… I’ve tried a variety of approaches without success. It displays info.html if I don’t localize it, but as soon as I add another language localization it gives a 260 error (cannot find the file). Any suggestions?
Jim, localization is not supported, but that’s a good idea … I’ll think about it! Marin
I ended up doing this when I call: [MTPopupWindow showWindowWithHTMLFile:NSLocalizedString(@"HTMLPROMO",nil)]; and then included various localizations for the file (e.g., index.html, index_es.html, index_de.html, index_fr.html, etc.). It works fine.
Now I have a different problem. Each index_xx.html file contains a link to more_xx.html as per your tutorial. These links also work fine. I get an error when trying to link outside the app (i.e. linking to iTunes to buy one of my other apps). The error is “..Document cannot be loaded, try again later”.
I’m pretty sure it’s because I need to distinguish between “internal” links and “external” links. I will look through your tutorial again to see if I missed that bit but feel free to make a suggestion!
Looking through .h and .m it’s clear that it’s not set up yet to “Close thySelf and then Launch an external website inside Safari”.
I think the new code should go here : if ([self.fileName hasPrefix:@"http"]) { : inside showInView, but am not sure how to do it elegantly.
Also, on the subject of Localization, my method works but some of the language characters (oomlahs, accents, etc.) are not displayed correctly in the MTPopupWindow view making some of the translations challenging to read. I’ll do my own workaround for now, but please let me know if you add this component!
Great !! Good job.
Nice – thanks for sharing!
Nice but a bit misleading. This is not a window but a view in the application window. I was looking for some info on how to create a window that is like an alert. A window that has a window level that allows it to be presented modally over what ever is the visible view, like an alert view or a keyboard. I don’t want to be concerned with whether a presented view is already present on the visible view or the naviagation bar or any where else. Anyway this is not that item This is not a window in the windows array of UIApplication.
All that said the tutorial is nice and u helped me to discover the JSON parser object last winter, so thanks. Perhaps this should be a popup view
[...] used my MTPopupWindow class to show credits and terms of [...]
Hi there, I do believe your blog could be having browser compatibility issues.
Whenever I take a look at your web site in Safari, it looks fine
however, if opening in Internet Explorer, it has some overlapping issues.
I merely wanted to give you a quick heads up! Besides that, wonderful website!
Hi Victoria, i’ve no need for internet explorer compatibilty
thanks for being concerned
Love it – I have been trying to figure out how to do this for weeks and you do it in the blink of an eye! Just one question – if I wanted to make the window smaller what is the best way to go about it? Thanks
[...] Hi this is Marin – the author of Touch Code Magazine, I hope you are enjoying my tutorials and articles. Also if you need a bright iPhone developer overseas contact me – I do contract work. Here's my LinkedIn profile NOTE: I have posted a newer version of this same tutorial, which makes use of modern iOS5 and iOS6 features.If you want a more flexible and modern code have a look at the more recent version, which you can find here: http://www.touch-code-magazine.com/showing-a-popup-window-in-ios6-modernized-tutorial-code-download/ [...]