2016-07-04 5 views
0

私は、スクロールビューで垂直方向に配置したい要素がいくつかあります。私はUILabel、UITextView、UIImageView、別のUITextView、別のUITextViewが続いています(scrollViewではヘッダー、イントロ段落、イメージ、本文段落、結論段落が必要です)。ページごとに異なる辞書や画像があるので、これらの要素のサイズをハードコードしてください。どのようにして各ビュー要素のフレームのCGRect浮動小数点サイズを適切に見つけることができますか?特にY値に問題があります。 !これは私が持っているコードです:。scrollviewのコンテンツのCGRectフレームサイジングに問題があるXcode

はstartingY =(?self.navigationControllerを.navigationBar.frame.height)聞かせて+ UIApplication.sharedApplication()statusBarFrame.size.height

//Position subViews on screen. 
    self.categoryTitle.frame = CGRectMake(0, startingY, self.view.bounds.width, 50) 
    let secondY = startingY + 50 
    self.categoryIntro.frame = CGRectMake(0, secondY, self.view.bounds.width, secondY + self.categoryIntro.contentSize.height) 
    let thirdY = secondY + self.categoryIntro.contentSize.height 
    self.imageView.frame = CGRectMake(0, thirdY, self.view.bounds.width, thirdY + image.size.height) 
    let fourthY = thirdY + image.size.height 
    self.categoryParagraph.frame = CGRectMake(0, fourthY, self.view.bounds.width, fourthY + self.categoryParagraph.contentSize.height) 
    let fifthY = fourthY + self.categoryParagraph.contentSize.height 
    self.categoryConclusion.frame = CGRectMake(0, fifthY, self.view.bounds.width, fifthY + self.categoryConclusion.contentSize.height) 

ありがとうございます!

答えて

0

yを格納する変数をインクリメントしたい場合は、varを使用する必要があります。 letキーワードは定数を保持したい変数のためのものです。

また、CGRectMakeの最後のパラメータにyの位置を追加する必要もありません。高さは、2番目のパラメータで設定されたyの位置に追加されます。

次に、y位置変数の最終値を使用して、スクロールビューのcontentsizeを更新できます。

ステータスバーの下にスクロールビューを配置している場合は、ゼロから開始します。あなたはその後、それぞれの高さを追加することができます

は、追加表示:

var yPos = 0; 
self.categoryTitle.frame = CGRectMake(0, yPos, self.view.bounds.width, 50) 
yPos += self.categoryTitle.frame.size.height; 
self.categoryIntro.frame = CGRectMake(0, yPos, self.view.bounds.width, self.categoryIntro.contentSize.height) 
yPos += self.categoryIntro.frame.size.height; 
self.imageView.frame = CGRectMake(0, yPos, self.view.bounds.width, image.size.height); 
yPos += self.imageView.frame.size.height; 
関連する問題