2016-12-26 6 views
0

オブジェクトの配列をスクロールするフルスクリーンスクロールビューを作成しようとしています。ストーリーボードでは、スーパービューの端に固定されたスクロールビューをフルスクリーンに表示します。配列内のオブジェクトごとにテキストフィールドを持つ新しいビューが作成されます(コード参照)。UIScrollviewの幅がiPhoneのサイズと同じになりません

スクロールビューは6sサイズでは完全に機能しますが、SEシミュレータで実行すると結果が異なります。スクロールビューは完全に下に

enter image description hereenter image description here

下記参照)までねじ込まれている私のscrollView設定されている:enter image description here以下

は私のコードです。その目的は、各オブジェクトのビューを作成し、そのテキストを表示することでした。

class ViewController: UIViewController { 

//Create your connection from your storyboard 
@IBOutlet weak var scrollView: UIScrollView! 

// This is an array of views that will be appended to the scroll view later. 
// These can be images or whatever want 
var mainViews = [UIView]() 

// This is an array of objects that are modeled off the SampleObjcet class we created earlier. 
// They all contain diffrent strings to display. We will add the text as they are created 
var arrayOfSampleObjects = [ 
    SampleObject(sampleText: "Page 1"),SampleObject(sampleText: "Page 2"), 
    SampleObject(sampleText: "Page 3"),SampleObject(sampleText: "Page 4"), 
] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // contentWidth is a varible that controlls the width of the scroll view 
    // This variabel grows each time a view is added 
    var contentWidth: CGFloat = 0.0 

    // scrollviewWidth & scrollviewHeight hold the sizes so we can use them to 
    // calculate the diffrent views later 
    let scrollviewWidth = scrollView.frame.size.width 
    let scrollviewHeight = scrollView.frame.size.height 

    // This is where the magic happens. 
    // This for loop goes through the array of objects and create views for each object. 
    for object in 0..<arrayOfSampleObjects.count { 

     // textToShowInformation is a label made to show the text from the diffrent objects. 
     // Sets up text properties 
     let textToShowInformation = UILabel() 
     textToShowInformation.text = arrayOfSampleObjects[object].sampleText // Goes through the arrayOfSampleObjects infromation 
     textToShowInformation.translatesAutoresizingMaskIntoConstraints = false 
     textToShowInformation.textAlignment = .center 

     // viewToShowInformation is the view that wil be created for each object 
     // for example, if there are 50 objects, there will be 50 views. 
     let viewToShowInformation = UIView() 

     // This adds the diffrent views to the mainViews array to be displayed 
     mainViews.append(viewToShowInformation) 

     // ERROR 
     // come back to this.... the calculations only work for 6s -_- 
     var newX: CGFloat = 0.0 

     //object in an index so if its object 4, 
     //it will be 4x' s as wide...i think... 
     newX = scrollviewWidth * CGFloat(object) 


     contentWidth += newX - scrollviewWidth 
     scrollView.addSubview(viewToShowInformation) 
     viewToShowInformation.addSubview(textToShowInformation) 
     viewToShowInformation.frame = CGRect(x: newX, y:0, width: scrollviewWidth, height: scrollviewHeight) 

     // Constraints for the text fields. Center Everything... 
     textToShowInformation.centerXAnchor.constraint(equalTo: viewToShowInformation.centerXAnchor).isActive = true 
     textToShowInformation.centerYAnchor.constraint(equalTo: viewToShowInformation.centerYAnchor).isActive = true 
    } 

    // Sets up the scroll view properties 
    scrollView.bounces = true // allows views to bounce past the scrollview width. change to personal preferance 
    // Below controlls the width size of the scrollview,I think the problem could lie here too 
    scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(arrayOfSampleObjects.count), height:scrollviewHeight) 
    scrollView.clipsToBounds = false // important 
} 
} 

すべては6-6sでは機能しますが、SEで実行されるとテキストは中央に配置されません。 object_controlled_scrollview

答えて

1

あなたはviewDidLoad内で次のコードブロックを実行している -

let scrollviewWidth = scrollView.frame.size.width 
let scrollviewHeight = scrollView.frame.size.height 

あなたのautoLayoutはまだ実装されていません。したがって、最初はscrollViewWidthscrollViewHeightは、あなたのプロジェクトをデザインしたストーリーボードのサイズに設定されます。

viewDidLayoutSubviewsまたはviewDidAppear

OR

のいずれかにUIScrollViewを設定し、コードのあなたの全体のブロックを移動してみてくださいあなたはsuper.viewDidLoad()

+0

私は他の方法を試していませんが、それをviewDidAppearに入れて固定しました!ありがとうございました! –

+0

'viewDidAppear'で関数が複数回呼び出されるので一度だけ実行するようにしてください! – Rikh

+0

'view.layoutIfNeeded()'が呼び出されました。代わりに使用します!ありがとうございました! –

0

てみ以下に追加:あなたはそれを試すことができるようにテキストは、あなたが明確にここでそれを参照してくださいカント場合

を追加され、このビューの中央部に固定され、ここでレポへのリンクですコードの行:

scrollView.setNeedsLayout() 
scrollView.layoutIfNeeded() 
+0

view.layoutIfNeeded()を呼び出す力を試すことができます、しかし、答えていただきありがとうございますこの答えはうまくいかない。 –

関連する問題