Background Image has partial white border

DaveCarsonDaveCarson GBUniversity ✭✭✭

I have a UIViewController and want to apply a background image to fill the entire screen. I have created the images and ensured that they are the correct size but when the View is displayed there is a small white border down the right hand side and along the bottom of the image.

I have tried applying the image using the following code:
View.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile (Constants.SectionBackgroundPath));

and using a UIImageView

var backgroundImageView = new UIImageView (UIImage.FromFile (backgroundImagePath)); View.AddSubview (backgroundImageView);

but the result is the same. I tried a screenshot but the border is not shown on the resulting image.

Note that the border is visible on the simulator and actual devices (4s, 5s and an iPad Air).

Obviously missing something here - any pointers greatly appreciated.

Best Answer

Answers

  • ashalvaashalva GEMember ✭✭✭
    edited November 2015

    Hi @DaveCarson,
    Try creating the view, which fits your screen, and then set the layer content of the view.
    var contentView = new UIView (new CGRect (0, 0, View.Frame.Width, View.Frame.Height)); contentView.Layer.Contents = UIImage.FromBundle ("mypicture.png").CGImage; View.AddSubView(contentView);

    Or more simply try setting the layer content of your Viewcontroller's view
    this.View.Layer.Contents = UIImage.FromBundle ("mypicture.png").CGImage;

  • DaveCarsonDaveCarson GBUniversity ✭✭✭

    Thanks @ashalva but I'm still getting the same result, a thin white line down the right and along the bottom of the view.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Where do you set the size of the image view?

  • DaveCarsonDaveCarson GBUniversity ✭✭✭

    The backgroundImageView is configured in a private method called from ViewDidLoad.

    I've tried passing in the View.Bounds into the UIImageView constructor (although I know that's not generally a good idea) and setting the .Frame property separately.

  • DaveCarsonDaveCarson GBUniversity ✭✭✭

    Thanks @adamkemp - I'll review this tomorrow when I'm back on this project

  • ashalvaashalva GEMember ✭✭✭
    edited November 2015

    Hi @DaveCarson,

    Due to the fact that the screenshot is not provided, my answer is the only a shoot. Maybe your titlebar has some effect on your view frame. Try calculating the titlebar height and starting your layer content from there.

    nfloat statusBarInfoHeight = UIApplication.SharedApplication.StatusBarFrame.Height; var height = (statusBarInfoHeight + this.NavigationController.NavigationBar.Frame.Height); var contentView = new UIView (new CGRect (0, height, View.Frame.Width, View.Frame.Height)); contentView.Layer.Contents = UIImage.FromBundle ("mypicture.png").CGImage; View.AddSubView(contentView);

  • DaveCarsonDaveCarson GBUniversity ✭✭✭

    I ended up moving my code to ViewDidLayoutSubviews and everything seems fine now.

    Thanks for the input and pointers

  • JVFLAJVFLA USMember ✭✭

    Could you possibly post a code snippet for other people in future that bump into this?

Sign In or Register to comment.