我添加了一个带有UIImage的UIImageView,我希望它出现在主UIView的中心顶部。
我将使用UIPageControl将该视图向左滑动并从右侧引入新视图。我添加了UIImageView,通常只是将它添加到右下角
-(void)createUIViews{
UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"];
firstView = [[UIImageView alloc] initWithImage:image1];
firstView.backgroundColor = [UIColor grayColor];
firstView.tag = 1;
firstView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
}
现在我希望将它定位在顶部中心。所以我想到这样的事情:
CGRect newFrame = self.view.frame;
newFrame.origin.x += 50;
self.view.frame = newFrame;
[self.view addSubview:firstView];
但是当我在前一个底部添加此代码时(从viewDidLoad调用btw),图像仍然显示在右下角。我该如何正确定位?
答案 0 :(得分:3)
将代码移至viewDidAppear。在viewWillAppear / viewDidAppear之前,不保证设置边界和框架。
答案 1 :(得分:1)
确保正确设置支柱和弹簧,然后执行此操作:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
firstView.center = self.center;
}