How to add multiple view controllers on scrollview without them overlapping - ios

I'm trying to fit multiple view controllers on a scroll view. But the views overlap on the screen
let settingView: SettingsView = SettingsView(nibName: "SettingsView", bundle: nil)
self.addChild(settingView)
self.scrollView.addSubview(settingView.view)
settingView.didMove(toParent: self)
settingView.view.frame = self.view.bounds
let mainView: MainView = MainView(nibName: "MainView", bundle: nil)
self.addChild(mainView)
self.scrollView.addSubview(mainView.view)
mainView.didMove(toParent: self)
mainView.view.frame = self.view.bounds
var mainFrame: CGRect = mainView.view.frame
mainFrame.origin.x = settingView.view.frame.width
mainView.view.frame = mainFrame
let connectionView: ConnectionView = ConnectionView(nibName: "ConnectionView", bundle: nil)
self.addChild(connectionView)
self.scrollView.addSubview(connectionView.view)
connectionView.didMove(toParent: self)
connectionView.view.frame = scrollView.bounds
var connectionFrame: CGRect = connectionView.view.frame
connectionFrame.origin.x = 2 * self.view.frame.width
connectionView.view.frame = connectionFrame
self.scrollView.contentSize = CGSize(width: (self.scrollView.frame.size.width) * 3
, height: self.scrollView.frame.size.height)
self.scrollView.contentOffset = CGPoint(x: (self.view.frame.width), y: self.view.frame.height)
each view controller should fit the size of the screen but they overlap onto each other screen

Related

How to set position and size of UIViewController in other UIViewController

I am doing this to move a ViewController(viewController) to other ViewController(mainMenuViewController) :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "subView")
addChildViewController(controller)
view.addSubview(controller.view)
controller.view.frame.size.width = 375
controller.view.frame.size.height = 198
controller.didMove(toParentViewController: self)
viewController get moved to mainMenuViewController but problem is that I can't see the the other items like tableView, button I have added to mainMenuViewController
Set frame instead of just height
controller.view.frame = CGRect(x: xPosition, y: y, width: width, height: height)
Also set autoreszingmaskToconstraint to false
controller.view.translatesAutoresizingMaskIntoConstraints = false;

how to add custom view in a middle of base view

I have created custom language view. I want that view show on a middle of center of iPad.
Here is what I have
static let langView = UINib(nibName: "LanguagesListView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! LanguagesListView
func languagesPopup() -> Void {
BaseVC.langView.delegate = self
BaseVC.langView.frame = self.view.bounds
BaseVC.langView.center = CGPoint(x:self.view.frame.size.width / 2, y:
self.view.frame.size.height / 2);
self.view.addSubview(BaseVC.langView)
}
When I call this method it shows somewhere in the left.
You're setting the frame of your langView equals to bounds of your root view, so the position x, position y, width and height will be same of your root view. If the width of langView is same of parent view your view always will be in left side. Try set different size for your langView. Try something like this:
override func viewDidLoad() {
super.viewDidLoad()
let viewT = UIView()
viewT.frame.size = CGSize(width: 50, height: 50)
viewT.backgroundColor = UIColor.red
viewT.center = self.view.center
self.view.addSubview(viewT)
}

View controller origin changes every time it's presented

This is what my view controller should be:
This is what it is sometimes:
I want to display a view controller in the circle, however, almost every time the view controller in the circle (ResultViewController) is presented, it's place is different, though its properties doesn't change at all. Here's my code:
func openCircle(withCenter center: CGPoint, dataSource: ([Items], Int, String)){
self.addCircle(withCenter: center, dataSource: dataSource)
}
func addCircle(withCenter circleCenter: CGPoint, dataSource: ([Items], Int, String)) {
let longerSide = fmax(view.frame.size.height, view.frame.size.width)
let shorterSide = fmin(view.frame.size.height, view.frame.size.width)
let circleRadius = longerSide / 2
var resultViewOrigin = CGPoint()
var resultViewSize = CGSize()
if UIDevice.current.userInterfaceIdiom == .pad {
let rectWidth = shorterSide / 2
let rectHeight = sqrt(abs(circleRadius * circleRadius - rectWidth * rectWidth)) + view.frame.size.height - circleCenter.y
resultViewSize = CGSize(width: rectWidth, height: rectHeight)
resultViewOrigin = CGPoint(x: (view.frame.size.width - rectWidth) / 2, y: view.frame.size.height - rectHeight)
} else {
resultViewOrigin = CGPoint(x: 0.0, y: 0.0)
resultViewSize = CGSize(width: view.frame.size.width, height: view.frame.size.height)
}
let resultViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ResultVC") as! ResultViewController
resultViewController.transitioningDelegate = self
resultViewController.modalPresentationStyle = .custom
resultViewController.dataSource = dataSource
resultViewController.view.frame = CGRect(origin: resultViewOrigin, size: resultViewSize)
transition.circle = UIView()
transition.startingPoint = circleCenter
transition.radius = circleRadius
transition.circle.frame = circleFrame(radius: transition.radius, center: transition.startingPoint)
present(resultViewController, animated: true)
}
It works well on the iPhone, not on the iPad, what's the problem?
I found the problem, it's actually a missing constraint on Regular-Regular size class caused this problem, I fixed it by adding a spacing to bottom layout guide to the part that used to get misplaced.
Thanks to everybody for your idea.
You can use a container view instead of presenting the view controller. You can create them programmatically or in interface builder (see Apple docs).

Jump to a specific subViewController in Swiping

I have implemented swiping. It has five subViews. Its working well normally.
Now, under a specific case, I need to go directly to fourth subViewController. After that swiping should not disturb user should be able to swipe forward and back as working before this jump.
How to handle this?
I have use these lines of code to implement swiping:
let vc0 = ViewController0(nibName: "ViewController0" ,bundle: nil)
self.addChildViewController(vc0)
self.myscrolview.addSubview(vc0.view)
vc0.didMoveToParentViewController(self)
let vc1 = ViewController1(nibName: "ViewController1",bundle: nil)
var frame1 = vc1.view.frame
frame1.origin.x = self.view.frame.size.width
vc1.view.frame=frame1
self.addChildViewController(vc1)
self.myscrolview.addSubview(vc1.view)
vc1.didMoveToParentViewController(self)
let vc2 = ViewController2(nibName: "ViewController2",bundle: nil)
var frame2 = vc2.view.frame
frame2.origin.x = self.view.frame.size.width * 2
vc2.view.frame=frame2
self.addChildViewController(vc2)
self.myscrolview.addSubview(vc2.view)
vc2.didMoveToParentViewController(self)
let vc3 = ViewController3(nibName: "ViewController3",bundle: nil)
var frame3 = vc3.view.frame
frame3.origin.x = self.view.frame.size.width * 3
vc3.view.frame=frame3
self.addChildViewController(vc3)
self.myscrolview.addSubview(vc3.view)
vc3.didMoveToParentViewController(self)
let vc4 = ViewController3(nibName: "ViewController4",bundle: nil)
var frame4 = vc4.view.frame
frame4.origin.x = self.view.frame.size.width * 4
vc4.view.frame=frame4
self.addChildViewController(vc4)
self.myscrolview.addSubview(vc4.view)
vc3.didMoveToParentViewController(self)
self.myscrolview.contentSize = CGSizeMake(self.view.frame.size.width * 5, self.view.frame.size.height-66)

iOS only half of view displaying

I have this problem. So I'm making this snapchat style menu with switching screens in a horizontal scroll view. All the view controllers are in 1 story board.
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let V1 = storyBoard.instantiateViewControllerWithIdentifier("FirstViewController")
let V2 = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController")
let V3 = storyBoard.instantiateViewControllerWithIdentifier("ThirdViewController")
let V4 = storyBoard.instantiateViewControllerWithIdentifier("FourthViewController")
self.addChildViewController(V1)
self.scrollView.addSubview(V1.view)
V1.didMoveToParentViewController(self)
self.addChildViewController(V2)
self.scrollView.addSubview(V2.view)
V2.didMoveToParentViewController(self)
self.addChildViewController(V3)
self.scrollView.addSubview(V3.view)
V3.didMoveToParentViewController(self)
self.addChildViewController(V4)
self.scrollView.addSubview(V4.view)
V4.didMoveToParentViewController(self)
var V2Frame : CGRect = V2.view.frame
V2Frame.origin.x = self.view.frame.width
V2.view.frame = V2Frame
var V3Frame : CGRect = V3.view.frame
V3Frame.origin.x = self.view.frame.width * 2
V3.view.frame = V3Frame
var V4Frame : CGRect = V4.view.frame
V4Frame.origin.x = self.view.frame.width * 3
V4.view.frame = V4Frame
self.scrollView.contentSize = CGSizeMake(self.view.frame.width * 4, self.view.frame.size.height)
However, as previously stated... only about half of each view shows....

Resources