Forum Xamarin.iOS

Setting Image Control Position/Width/Height in Codebehind

KrisAdamsKrisAdams GBMember ✭✭

Hey. I am coming from a WPF background and have just started working in Visual Studio with Xamarin and need some help with the layout.

Here is my scenario:
I want an Image Control on the page. I want to be able to control its Position and Width/Height manually (this is based on some criteria which isn't important right now).

So what I have done so far is try to create a very simple test, but can't get it to change to size/position of the control (it shows the image fine).

I just went onto the designer, dropped a UIImageView on the page (just in the middle for now). Then in the codebehind in ViewDidLoad() I am trying to change the position and size.

I tried:
ImageView.Frame = new CoreGraphics.CGRect(0, 0, 30, 60);
ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;

But it doesn't change it at all.

My question is: What do I need to do to be able to manipulate this object in the code-behind?

Any help would be appreciated.

Cheers,
Kris

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You probably have constraints that are overriding what you did here. If you edit things in the interface builder then it tends to automatically create constraints for you, which in this case is not what you want. Try creating the image in code.

    You can verify in the debugger that it has constraints by looking at the Constraints property.

  • KrisAdamsKrisAdams GBMember ✭✭

    Ah, interesting. I will give that a go when I get home.
    So in your opinion, creating the layouts in code is maybe a safer option when wanting to manipulate them?

    The "Inferface Builder" feels a lot like Windows Forms to me. I think WPF/Xaml is a much better upgrade from Winforms. I wish that the ios design style was also upgraded to be XML based.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    If you want to interactively move an element around then it's best not to use constraints, and the easiest way to avoid constraints is to not use interface builder. Alternatively, you can make the constraints in interface builder explicit, give the constraints names (I.e., connect them to outlets), and modify the constraints in UpdateViewConstraints. That seems like a hassle for a case like this, but makes sense in a lot of other cases.

Sign In or Register to comment.