Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8975829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:58:44+00:00 2026-06-15T18:58:44+00:00

I’m coding a generig Monotouch.Dialog element to display an Image (that doesn’t display the

  • 0

I’m coding a generig Monotouch.Dialog element to display an Image (that doesn’t display the image picker), I’m reusing code from UIViewElement and ImageElement, added some parameters so it can be usable in a login screen for instance (showing company logo, etc).

I have this so far:

public class ImageViewElement : Element, IElementSizing
{
    protected UIImage Image;
    public CellFlags Flags = 0;
    public UIViewContentMode Alignment;

    public enum CellFlags {
        Transparent = 1,
        DisableSelection = 2,
        Both = 3
    }

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="caption">
    /// The caption, only used for RootElements that might want to summarize results
    /// </param>
    /// <param name="view">
    /// The view to display
    /// </param>
    /// <param name="transparent">
    /// If this is set, then the view is responsible for painting the entire area,
    /// otherwise the default cell paint code will be used.
    /// </param>
    public ImageViewElement (UIImage image, bool transparent, bool selectable, UIViewContentMode alignment) : base ("")
    {
        this.Alignment = alignment;
        this.Image = image;

        if(transparent)
            {Flags+=1;}
        if(!selectable)
            {Flags+=2;}
    }


    public override UITableViewCell GetCell (UITableView tv)
    {
        var cell = tv.DequeueReusableCell (CellKey);
        if (cell == null)
        {
            cell = new UITableViewCell (UITableViewCellStyle.Default, CellKey);
            cell.ContentMode = UIViewContentMode.BottomRight;
            switch(Flags)
            {
            case CellFlags.Transparent:
                cell.BackgroundColor = UIColor.Clear;
                //
                // This trick is necessary to keep the background clear, otherwise
                // it gets painted as black
                //
                cell.BackgroundView = new UIView (RectangleF.Empty) {BackgroundColor = UIColor.Clear};
                break;
            case CellFlags.DisableSelection:
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
                break;
            case CellFlags.Both:
                cell.BackgroundColor = UIColor.Clear;
                //
                // This trick is necessary to keep the background clear, otherwise
                // it gets painted as black
                //
                cell.BackgroundView = new UIView (RectangleF.Empty) {
                    BackgroundColor = UIColor.Clear};
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
                break;
            }
            cell.ImageView.ContentMode = Alignment;
            cell.ContentMode = Alignment;
            cell.ImageView.Image = Image;

        }
        return cell;
    }
    public float GetHeight (UITableView tableView, NSIndexPath indexPath)
    {
        return Image.Size.Height;
    }
}

Everything works so far with exception of the alignment, the Image is always displayed left, I tried setting ContentMode on the cell and on the cell’s Imageview but it’s not working, what can I do?


EDIT: Miguel pointed out my bounds are probably not set correctly, so I tried this:

cell.ContentMode = UIViewContentMode.Right;
var test = new UIImageView(Image);
cell.ContentView.Add (test);

Now my ContentView bounds are same as cell bounds, 0,0,320,40, they are not ok but I’m only looking for with, my test bounds are 0,0,256,256, but alignment is still not working… dunno what to do


EDIT: Some progress!!!

I asked for advice on IRC

(2012-11-13 10:10:12) CENSORED: Implement a subclass of UITableViewCell
(2012-11-13 10:10:26) CENSORED: add the Image as a subview in the init method of the new class
(2012-11-13 10:10:33) CENSORED: and override layouSubviews
(2012-11-13 10:10:36) CENSORED: with the line:
(2012-11-13 10:10:56) CENSORED: imageView.Center = this.Center;
(2012-11-13 10:11:14) CENSORED: (call super first as well)
(2012-11-13 10:11:25) CENSORED: that way the image will always remain centred in the cell

(2012-11-13 10:38:23) CENSORED: AndreSM: UITableViewCell already has a UIImageView property (named ImageView)
(2012-11-13 10:38:36) CENSORED: you can just use that instead of creating a new one

So I went ahead and created a subclass:

public class ImageViewCell: UITableViewCell
{
    UIImage cellImage;

    public ImageViewCell (UIImage image, NSString key) : base(UITableViewCellStyle.Default, key)
    {
        cellImage = image;
        ImageView.Image = cellImage;
    }

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        ImageView.Center = ContentView.Center;
        ImageView.SetNeedsDisplay ();
    }
}

But image is still a bit off center, here is a screenshot so you can get what I mean:

enter image description here

enter image description here

I don’t get the reason for this, I checked what is the new center and the value is correct (160 in the X axis..)

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:58:45+00:00Added an answer on June 15, 2026 at 6:58 pm

    The problem is that Center is defined in the PARENT coordinate space. So the Center property of the ContentView is defined in the space of its PARENT (the UITableViewCell, which is a view itself). but the ImageView center is defined in the coordinate space of the parent of the imageView which is the ContentView.

    This code should solve it by calculating the center in the contentviews own coordinate space

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        ImageView.Center = new PointF(ContentView.Bounds.Width/2,ContentView.Bounds.Height/2);   
        ImageView.SetNeedsDisplay ();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.