簡體   English   中英

iOS在viewDidLoad上添加子視圖

[英]IOS Add subview on viewDidLoad

當我嘗試向viewDidLoad上的UIScrollView添加子視圖時出現問題。

我正在使用以下代碼以編程方式將UIImageViews添加到scrollView:

- (void)viewDidLoad {
[super viewDidLoad];
NSInteger const_width = 100;
NSInteger numberOfViews = 4;
CGRect theFrame = [self.scrollView frame];
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * const_width;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y,const_width,110)];
    UIImage *image = [UIImage imageNamed:@"cocoloco.jpg"];
    [imageView setImage:image];
    //[self.scrollView addSubview:imageView];
    imageView.tag = i;
    CGRect rect = imageView.frame;
    rect.size.height = 110;
    rect.size.width = 110;
    imageView.frame = rect;
    [self.scrollView addSubview:imageView];

}

self.scrollView.contentSize = CGSizeMake(const_width * numberOfViews, 110);}

但我得到當前的看法:

在此處輸入圖片說明

似乎無論3個黃色的選項卡(這是一個特殊的TabBarController),滾動視圖框架都處於其位置,所以我從UIScrollView獲得了錯誤的框架原點,因此UIImageViews的位置是錯誤的。

任何想法?

我不知道該怎么做,但是將框架的高度添加到矩形的“ Y”位置。 像這樣:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y + (theFrame.height),const_width,110)];

要使滾動視圖位於頂部導航欄下方,請嘗試

self.scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

然后通過基於scrollview的原點和高度設置其原點,將表放置在scrollview的下方:

CGRect frame = tableView.frame;
frame.origin.y = self.scrollview.frame.origin.y + self.scrollview.frame.size.height;
tableView.frame = frame;

您應該將UI的任何大小調整或布局移至-(void)layoutSubviews方法,這應該可以對問題進行正確排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM