【问题标题】:How do I position UIView in superview?如何在超级视图中定位 UIView?
【发布时间】:2013-03-12 20:34:32
【问题描述】:

我正在添加一个带有 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 调用的),图像仍然出现在右下角。如何正确定位?

【问题讨论】:

    标签: ios uiview uiimageview frame bounds


    【解决方案1】:

    将您的代码移至 viewDidAppear。在 viewWillAppear/viewDidAppear 之前,不能保证已设置边界和框架。

    【讨论】:

    • 好的,现在我需要从 UIPageControl 移动它的动画,我应该再发一个帖子吗?
    • 是的,我会再发一篇关于动画的帖子,因为这是一个更复杂的过程。
    【解决方案2】:

    确保正确设置支柱和弹簧,然后执行以下操作:

     -(void)viewWillAppear:(BOOL)animated 
      {
        [super viewWillAppear:animated];
        firstView.center = self.center;
      }
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 2013-07-11
      • 2015-02-07
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 2017-03-29
      • 2016-06-29
      相关资源
      最近更新 更多