IOS auto-layout limitations and framework not working

I have a scroll view nested inside a view controller that resides in a container. The view controller that uses the specified class called ScrollingViewController is as follows:

 class ScrollingViewController: UIViewController { @IBOutlet weak var scrollView: UIScrollView! //outlet for the Scroll View override func viewDidLoad() { super.viewDidLoad() // 1) Create the two views used in the swipe container view var storyboard = UIStoryboard(name: "App", bundle: nil) var subOne: SubProfileOneViewController = storyboard.instantiateViewControllerWithIdentifier("subone") as! SubProfileOneViewController var subTwo: SubProfileTwoViewController = storyboard.instantiateViewControllerWithIdentifier("subtwo") as! SubProfileTwoViewController // 2) Add in each view to the container view hierarchy // Add them in opposite order since the view hierarchy is a stack self.addChildViewController(subTwo); self.scrollView!.addSubview(subTwo.view); subTwo.didMoveToParentViewController(self); self.addChildViewController(subOne); self.scrollView!.addSubview(subOne.view); subOne.didMoveToParentViewController(self); // 3) Set up the frames of the view controllers to align // with each other inside the container view var adminFrame :CGRect = subOne.view.frame; adminFrame.origin.x = adminFrame.width; subTwo.view.frame = adminFrame; // 4) Finally set the size of the scroll view that contains the frames var scrollWidth: CGFloat = 2 * self.view.frame.width var scrollHeight: CGFloat = 262 self.scrollView!.contentSize = CGSizeMake(scrollWidth, scrollHeight); // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Basically, what happens is that two view controllers that use this class SubProfileOneViewController and SubProfileTwoViewController , respectively, are created as subOne and subTwo . They are then added to the scroll list as children, to create an interface in which the user can scroll directly to access another view (almost like Snapchat). subOne and subTwo should be next to each other, and the user should be able to switch from one to the other and vice versa.

Here's what it all looks like on my storyboard: enter image description here

SubProfileOneViewController and SubProfileTwoViewController each have a view (represented by green and red, respectively), and each of them has the same exact limitations as: Height = 262, Trailing space to superview = 0, Leading space to superview = 0, Top space for the supervisor = 0

Ideally, when starting up, there should be two views, one green and one red, and the user should be able to scroll between them. However, here's what actually happens: enter image description here

Green and red views do not occupy the entire width of the screen and instead condense into a small ribbon on the left, and most view controllers are white, not their corresponding color. I have tried many things and I am not sure what I am doing wrong.

(credit for the code in ScollingViewController goes to lbrendanl on github, linked here: https://github.com/lbrendanl/SwiftSwipeView )

+5
source share
2 answers

Note. . I would recommend using an Autolayout solution that automatically handles orientation changes, screen sizes, all with much less code and a more robust solution than what I'm developing below.

When using the current approach, using the built-in UIViewControllers , you must wait for these controllers to complete the appropriate initializations, which are in direct competition with your layout. Please note that when changing the orientation you will need to recalculate the positions + sizes. Again, while this works, this is not a sound design, as it requires a lot of code, logic, and common functions that you could get for free using NSLayoutConstraint .

(checked, built, connected and launched):

 override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // 3) Set up the frames of the view controllers to align // with each other inside the container view var adminFrame = self.view.frame subOne.view.frame = adminFrame adminFrame.offset(dx: self.view.frame.width, dy: 0) subTwo.view.frame = adminFrame; // 4) Finally set the size of the scroll view that contains the frames let scrollWidth = 2 * self.view.frame.width self.scrollView!.contentSize = CGSizeMake(scrollWidth, self.view.frame.height) } 

Here is the scroll (in action). Please note that I created views of the ViewLayoutAssistant.swift class, which makes it very convenient to visualize the position + scale.

enter image description here

You also do not need to indicate that its hierarchy has changed. This last bit of code should catch you:

 @IBOutlet weak var scrollView: UIScrollView! weak var subOne: SubProfileOneViewController! weak var subTwo: SubProfileTwoViewController! self.addChildViewController(subTwo); self.scrollView!.addSubview(subTwo.view); self.addChildViewController(subOne); self.scrollView!.addSubview(subOne.view); 
+2
source

ScrollView with auto-layout works differently, or you can use only one subview by setting translatesAutoresizingMaskIntoConstraints = true and explicitly setting contentSize. Or you set translatesAutoresizingMaskIntoConstraints = false and let it determine the constraint on its own. See the link for more details.

How do you want both to display in full screen mode, you need to add AutoLayoutConstraints, something like this

  • GreenView icon on the left for superview
  • Set the width of the green screen in the same way as scrollViewWidth ie bounds.size.width (not contetnSizeWidth)
  • It will open in red to the left to the green screen on the right.
  • set the width value of redView in the same way as scrollViewWidth ie bounds.size.width (not contetnSize.width)
+2
source

All Articles