How to change the position and size of a UIScrollView?

时间:2015-06-25 18:14:02

标签: ios swift cocoa-touch uiscrollview

I got frustrated with AutoLayout so I decided to disable it and just set the positions and sizes of the elements in my view programatically. I tried this:

func setScrollViewBounds() {
    var windowFrame = self.view.frame;

    var scrollerHeigth = (windowFrame.height/100)*15

    BottomScroll.frame = CGRect(x: CGFloat(10), y: windowFrame.height-scrollerHeigth, width: windowFrame.width-20, height: scrollerHeigth);
    TopScroll.frame = CGRect(x: CGFloat(10), y: BottomScroll.frame.maxY-10, width: windowFrame.width-20, height: scrollerHeigth)
}

Im trying to set the two scroll views (TopScroll and BottomScroll) to be at the bottom of the page and have one be on top of the other. This however doesn't seem to do anything. Any ideas why?

I'm calling the function in ViewDidLoad(). TopScroll and BottomScroll are IBOutlets.

1 个答案:

答案 0 :(得分:0)

Apply constraint following order Put your subviews in UIView Object(Which will subview of ScrollView). Select Both ScrollView and UIView Go->Pin->Equal Width.(Not Equal Height) Select UIScrollView Go-> Pin uncheck Constraint to Margin and give Top,Bottom,Left,Right constraint. Now Select UIView(Subview of UIScrollview say contentView) control click contentView to Main View(Default controller's View) and select Leading,Trailing,Top,Bottom constraint. Finally Select contentView Go-> Resolve Auto-layOut issue -> Reset to Suggested constraints. Constraints for subview of ContentView as usual with respect to contentView. use viewDidLayoutSubview for setting content-size to UIScrollView.