UIImageView没有响应居中

我正在尝试为iOS设置一个简单的指南针。 旋转animation似乎围绕屏幕中心旋转指南针图像,所以我正在尝试自动居中指南针图像的UIImageView,以便它可以在不同的设备上工作。

这是我的问题所在。 我试图用ViewDidLoad中的下面的代码中心图像,但没有结果:

compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)); 

我最初把罗盘图像放在故事板内的一个随机的地方,以确保这个工作。 我已经打印NSLog检查之前和之后这个居中代码,以确保它是有效的,它似乎是居中图像(在我的iPhone 4s),但这并不反映在视图中:

 2014-05-17 12:18:23.015 Compass[447:60b] Old: 160, 386 2014-05-17 12:18:23.019 Compass[447:60b] New: 160, 240 

我会张贴一个图像,但不被允许,因为这是我的第一篇文章。

事实上,这整个设置。 我确定我在某个地方出了问题:

ViewController.h

 #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import <QuartzCore/QuartzCore.h> @interface ViewController : UIViewController <CLLocationManagerDelegate> @property (nonatomic, retain) CLLocationManager *locationManager; @property (nonatomic, retain) IBOutlet UIImageView *compassImage; @property (nonatomic, retain) IBOutlet UILabel *trueHeadingLabel; @end 

ViewController.m

 #import "ViewController.h" @interface ViewController() @end @implementation ViewController @synthesize locationManager, compassImage, trueHeadingLabel; - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Old: %i, %i", (int)compassImage.center.x, (int)compassImage.center.y); compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)); NSLog(@"New: %i, %i", (int)compassImage.center.x, (int)compassImage.center.y); locationManager=[[CLLocationManager alloc] init]; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.headingFilter = 1; locationManager.delegate=self; // Start the compass updates [locationManager startUpdatingHeading]; } - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { // Convert Degree to Radian // Multiply by -1 to twist opposite to phone rotation float oldRad = -manager.heading.trueHeading * M_PI / 180.0f; float newRad = -newHeading.trueHeading * M_PI / 180.0f; // creating needle spin animation CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"]; theAnimation.fromValue = [NSNumber numberWithFloat:oldRad]; theAnimation.toValue=[NSNumber numberWithFloat:newRad]; theAnimation.duration = 0.5f; // applying the animation [compassImage.layer addAnimation:theAnimation forKey:@"animateMyRotation"]; compassImage.transform = CGAffineTransformMakeRotation(newRad); // setting labels to heading trueHeadingLabel.text = [NSString stringWithFormat:@"%i°", (int)(newHeading.trueHeading)]; // console print of heading NSLog(@"True heading: %f", newHeading.trueHeading); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end 

我已经看过几乎每一个问题和答案在这里与改变一个UIImageView的位置和中心任何事情,没有任何工作正在做。 我觉得这可能就是我设立整个项目的方式,因为我是一个新的,缺乏经验的人,所以这不会是一个惊喜。 任何帮助将是太棒了。 甚至有更好的编程实践的build议。 如果你需要更多的信息gimmie喊。

听起来像你可能有自动布局启用,并覆盖您的代码。

更改:

 compassImage.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)); 

至:

 compassImage.center = self.view.center; 

另外,请确保您对IB中的compassImage没有任何限制,这会干扰。