Sunday 18 January 2015

Xamarin Forms–Using background images on iOS

Following on from my last post on creating A Light status bar for iOS, I wanted to go one step further and add in a full-screen background image to my application. This is very easy without a navigation bar, but with one it’s a bit more involved so I thought a blog post would be in order.

If you have no navigation bar then all you really need to do is create an image of the right size (for each physical phone you want to support, such as 4S, 5S, 6, 6 Plus), add images to the resources directory and use them in code or XAML. With a navigation bar it’s a bit more involved as you need to split your source image into two separate images (one for the navigation bar and one for the remainder of the screen).

Then when you have the images defined you need to hook the images to the correct controls, and this is where it gets a little more complex. With a NavigationPage we need to set the navigation bar’s background to an image – but we can’t do this in regular Xamarin Forms code as the property is only available in a custom renderer, so we’ll need to create one.

First off though we need an image – I chose one I took in 2006 whilst on holiday with my family – this is the last time we were all together and it was a beautiful day. Anyhow, I extracted four images in total as shown in the following…

Original Marked Up

The red outline contains the navigation bar image (640px * 128px) and the background image (640px * 1008px) – these are both for an iPhone 5S which is what I have to develop on at the moment.

The green outline contains the two images for the landscape view, the navigation bar being 1136px * 64px, and the background image being 1136px * 576px. The navigation bar is shorter by default in landscape mode as the status bar is switched off. I believe it’s possible to override this and display the status bar, but I don’t need that so haven’t looked into it further.

I named those images as follows…

  • PortraitNavBar@2x.png
  • PortraitBackgrouns@2x.png
  • LandscapeNavBar@2x.png
  • LandscapeBackground@2x.png

With that done I then needed to write a bit of code. First up I had to ensure that the correct background image was shown for the content page – this was simply a job of overriding the OnSizeAllocated member of my content page…

    protected override void OnSizeAllocated (double width, double height)
base.OnSizeAllocated (width, height);

if (width < height)
this.BackgroundImage = "PortraitBackground.png";
this.BackgroundImage = "LandscapeBackground.png";


The OnSizeAllocated method is called when the size changes on the phone and it provides the size of the page, so this seemed like a reasonable place to determine which image to load up.

Then I needed to add in a custom render so added a deriver navigation page as shown below…

    public class CustomNavigationPage : NavigationPage
public CustomNavigationPage (Page content) : base(content)

With that defined I could then add the renderer. The purpose of this renderer is to render the background image of the navigation bar...

[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(XamFormsTestbed.iOS.Renderers.CustomNavigationPageRenderer))]

namespace XamFormsTestbed.iOS.Renderers
public class CustomNavigationPageRenderer : NavigationRenderer
public override void ViewDidLoad ()
base.ViewDidLoad ();

var img = UIImage.FromBundle ("PortraitNavBar.png");
this.NavigationBar.SetBackgroundImage (img, UIBarMetrics.Default);

img = UIImage.FromBundle ("LandscapeNavBar.png");
this.NavigationBar.SetBackgroundImage (img, UIBarMetrics.Compact);

Originally I'd tried responding to size changes here, but then read an article that discussed the above mechanism and it appeared to work.

With all that in place I can now display a background image in the navigation bar and also have a correctly formatted image for both landscape and portrait orientations...



And there you have it – a full background image with a navigation bar. Hopefully this will help someone with their application.


«Oldest   ‹Older   1401 – 1443 of 1443
Canon Support said...

If you are using a Xerox printer and showing a Xerox printer offline error, you can try these steps to fix it.
Your printer might appear offline if it can't communicate with your PC.

• Select the Start icon on the bottom left of your screen,
• Select Control Panel, and then select Devices and Printers.
• Next, right-click the printer in question and select See what's printing.
• From the window that opens, select Printer from the menu bar at the top.
• Next, choose Printer Use Online from the drop-down menu.

It is the most straightforward method to fix this error. However, if you cannot do this, you can connect with us to fix this error.
Xerox Printer Offline

Website Design Digital Marketing Company Coimbatore said...

SEO Company in Coimbatore SEO Agency in Coimbatore SEO Services in Coimbatore
SEO company in coimbatore
digital marketing company in coimbatore digital marketing agency in coimbatore SEO company
Digital marketing company in coimbatore

jerry said...

Snapchat users finally have a piece of good news to cheer upon!. Read this complete article at The Pakistan.

Juliany said...

Permainan Sportsbook Online Terbaik memanglah sangat menyenangkan untuk di mainkan oleh bettor dan dapat memperoleh keuntungan cukup besar.

Nyatanya pasaran sportsbook sepakbola ini pasti tidak hendak jauh dari permainan olahraga sepak bola bukan? Di mana memanglah permainan sepak bola ini telah di favoritkan oleh para masyarakat, tidak cuma itu sudah sangat mendunia sekali.

Sam Smith said...

This article has it all thanks for sharing. The help to write any type of document requires numerous things to consider and a lot of time too. The final services of writers would be an exception for you. Because they are fully trained and educated. You cannot expect silly mistakes or defects from them in written tasks. The group of researchers conducts an in-depth investigation by collecting relevant data from the best possible sources. Read more on Capstone Project Writers

Alex Kim said...

Nice blog!This blog gives really important information.Thanks for sharing.Certified Translator

Unknown said...

Need more advanced set of wallpapers with updated pixels create music streaming app

Alex Kim said...

Singapore Translators is the best translation service provider in Singapore where a great team of capable and experienced translators offers human-based language translation for various kinds of documents like legal, medical, business, academic, and many more at a very reasonable price.

Simone Watson said...

The strength an article should possess appears in your post. Nowadays, security and privacy are the utmost in this digital world. An informative as well as creative post. Businesses looking for highly functional native applications can Blockchain software development company like AppSquadz can help with it.

Simone Watson said...
This comment has been removed by the author.
Assignment Help Pro said...

Hi, I am Linnea! If you need help with your assignment, I recommend you get online assistance. The Best assignment help we provide gives them good grades. Before paying for an assignment help service, feel free to get a free sample from our assignment helper. If you run into any issues, I am available to assist you.

NURSING said...

The Doctor of Nursing Practice (DNP) program is an advanced nursing degree that helps take one’s career to the next level. Students who have graduated in a DNP program are considered to have attained more leadership qualities and highly qualified health care practitioners. Earning your DNP degree provides skills and tools that enable you to examine published evidence to inform your practice, enhance systems of care to influence patient outcomes, and make changes to improve the quality of care when needed. Read more from DNP Assignment Writers

Leo Oliver said...

This is a fantastic post that is both knowledgeable and educational. Thank you so much for letting us in on your wonderful thoughts. If you're not sure whether or not to use our assignment writing service, contact one of our highly qualified and experienced authors for advice. Our Online Assignment Help service will be available to all students 24 hours a day, 7 days a week.

Essien said...

Thanks for informative articles, it’s an interesting updates. Looking forward to more of the updates on next publish. Its a better growth and hard work. Thanks so much for sharing. Meanwhile, visit latest news on rhema university post utme form

suraj said...

Absolutely an informative post, I am a student and share some valuable information for others to get matlab assignment help across the Globe. I found this which provides world class Matlab Project Help , MATLAB IMAGE PROCESSING ASSIGNMENT HELP , MATLAB ELECTRICAL ASSIGNMENT HELP to students globally. Best of Luck!

Essay Master UK said...

hire best writers from UK site and score well in exams with guaranteed results.
do my essay UK
buy cheap essay

Jyoti Verma said...

Hello Writer, such a nice article written.
Good job thanks for posting such good content, We have a skilled professional digital marketing team with 4+ years of experience. Also, our team gives 100% support every time. If you have any requirements regarding Digital Marketing Company then you can contact us at +91-8574546649

Aliamaxa said...

Thetuorshelp Finance homework help Finance homework help is prepared to help the students in understanding all the important concepts which are involved in financing. In finance homework we will deal with all the basic and important aspects of finance
Finance Homework Help

Unknown said...

We appreciate go work and for sharing such informative content. Would you be interested in nursing essay help in your assignment from our trained experts that will deliver content that is plagiarism free, Read more from Nursing Essay

I Learn Education said...

Amazing one!
Worth Stuff thanks for sharing.

Scholarship practice test Victoria and NSW
free for Australian children's to improve studies and carriers

Tracking2u said...

Superb post!!! worth it

GPS vehicle tracking system in Coimbatore

SFA Body Repairs said...

Excellent post!!!

Smash repair Dandenong

Franticpro said...

Hi, I am John Smith I am Web Developer, It is an amazing blog thanks for the sharing the blog. Frantic infotech provide the ios real estate app development such as an information about software development for costumer service. Frantic infotech also provide the ios event app development. Theve delopment of advanced web applications is Orient Software’s specialty and we will successfully fulfill all your web application development requirements, from small-sized to wider-ranged projects.

ved said...

Thank You For great Sharing information. once again thank you.

Checkpoint Endpoint Security VPN

Please visit above link for best antivirus and related information. Please check above link.

Neelp120 said...

Purchase the best best fitness tracker device only in 2000 in India. Offer limited
get the tracker now. Best deal to buy fitness tracker. Get best fitness band in india under 2000 to kickstart your fitness in cheapest price. Buy the best fitness band in India. best fitness band in india under 2000

3d Rendering China said...

Nice post.
3d walkthrough china

3drenderingindia said...

Such a very useful article.Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
3D Interior Design

best write my essay said...

we at write my essay for me in UK offer online essay help service with affordable price.

Aulia said...

Ami Bestie said...

Monica said...

3d Rendering China said...

Get high-quality 3D Rendering service in China with timely delivery, and lowest quote challenge for 3D Architectural rendering for Architects & Developers, 3d interior designs china, 3D Animation Movie, 3D Product Design & Modeling for Manufacturers, 3D Medical Illustration and Visualization.

Online Assignment Help said...

Essay writing is a part of academic study and every Academics student has to write a different essay as their assignment. Essay writing is not a cup of tea for everyone as it is quite different from other writing. Too much assignment pressure on essay writing hampered the concentration of the academic students. If you are one of them then contact a custom writer to have the best content in essay writing. If you are thinking about who had to find the best solution for essay writing then contact the experts of essay writing help. In exchange for a pocket-friendly service charge, the expert writers of this website will bring you the best essay content without any grammatical errors. No other solution will be best for you as with the help of the expert writers of this website you can handle both assignment pressure and your studies simultaneously. So do not waste your valuable time just contact the experts on this website. Surely will climb the success ladder with the help of expert writers on the essay writing help website.
Essay Writing Help

sifon said...

Excellent for this specific beneficial blog post. I am surprisingly happy to seek out this form of relevant information. geography jamb past questions & solutions in pdf

sifon said...

I have been looking for this blog post for use. I am grateful that you have share this useful contents. past questions for social studies pdf

YouTube to mp3 converter said...

hello are you looking porn website so don't worry our website free porn video downloads so join our site for free video downloads

Global Assignment Expert said...

If you need cheap online assignment help? Then Global Assignment Help offers the most affordable services with top-quality assignment help in all over the world. Our superior quality and 24*7 Expert assistance is very helpful to the students. Our native skilled experts cover various assignments like management assignment help, computer network assignment Help, programming assignment Help, engineering assignment help, and many more.

Global Assignment Expert said...

Global Assignment Expert provides various facilities such as Expert Guidance, Improved Grades, Customised content, Plagiarism-Free Work, Stress Reduction, and many more. Our Online Assignment Help services are such a valuable resource that can enhance your academic life. They will offer a Learning opportunity and help you manage your time. If you want to improve your grades then Global Assignment Help can be a game-changer in your academic journey.

Global Assignment Expert said...

Global Assignment Expert provides various assignment help online Finance Assignment Help is one of them. It comes under accounts, which means wealth management in other terms managing money. The assignment expert must have knowledge of accounts as well as having experience in finance. To complete the assignment we have highly experienced experts. We offer our assignments at an affordable price range. Global Assignment Help also provides premium services in terms of quality and price.

zara8359 said...

i read your article to end and it is very informative i applaud your endeavors. washing machine repair Dubai

zara8359 said...

your blog is quite informative i really find it useful i hope you will share more in future too, keep writing keep sharing.
Dubai Freelance Visa
Freelance Visa Dubai
Dubai Freelance Visa

zara8359 said...

well i hope everyone here will find your post informative like as much as i do, i appreciate your writing skills, keep up.
Company Formation In Dubai
Business Setup in Dubai

zara8359 said...

i am very fond of your writing skills the way you wrote these blogs is highly creative, i appreciate that.
Dubai Visa
Dubai Visa
Turkey Visa from Dubai

«Oldest ‹Older   1401 – 1443 of 1443   Newer› Newest»