MonoTouch: How to change control's location at

2019-09-08 07:02发布

问题:

I want to change the location of an UIImageView at runtime. In IB, I can change X coordinate to relocate it. So, I tried this code:

imageView.Frame.X = 25;

But it doesn't have any effect. Why? I have the same problem for other controls as well. How to make it work?

回答1:

The Frame property is a value type, this means that if you do:

imageView.Frame.X = 25

what you are actually doing is:

var temp = imageView.Frame;
temp.X = 25;

The value never reaches the imageView. With value types, you have to assign a fully constructed type, so for example:

var current = imageView.Frame; current.X = 25; imageView.Frame = current;



回答2:

The Frame property is a value type, this means that if you do:

imageView.Frame.X = 25

what you are actually doing is:

var temp = imageView.Frame; temp.X = 25;

The value never reaches the imageView. With value types, you have to assign a fully constructed type, so for example:

var current = imageView.Frame; current.X = 25; imageView.Frame = current;



回答3:

// The Code is quite simple and self explanatory

dispatch_async(dispatch_get_main_queue(), ^{

    self.myimageView.frame = CGRectMake(140, 70, 140, 128);


});

By using >dispatch_async(dispatch_get_main_queue(), ^{ I made the code to run in the main queue, not in the background.