[英]How do I position UIView in superview?
我添加了一個帶有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),圖像仍然顯示在右下角。 我該如何正確定位?
將代碼移動到viewDidAppear。 在viewWillAppear / viewDidAppear之前,不保證設置邊界和框架。
確保正確設置支柱和彈簧,然后執行以下操作:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
firstView.center = self.center;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.