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....
Related
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
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;
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).
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)
In a view I want to make a textView and a toolbar go up when the keyboard appears but I am having some issues because after moving them up.The issue is that the buttons on the toolbar can not be pressed anymore
I have to mention that I added these two items inside a view[that is inside a viewController-the main view of the page]
Here is what I tried:
func keyboardDidShow(notification :NSNotification){
var c = notification.userInfo as NSDictionary
var point:NSValue = NSValue(nonretainedObject: c.objectForKey(UIKeyboardFrameEndUserInfoKey))
let s:NSValue = c.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.messageText.frame
var toolbarFrame = self.toolbar.frame
var textFrame = self.textfieldFrame.frame
var offset = (rect.height - ((self.view.frame.height - self.messageText.frame.origin.y) + self.messageText.frame.size.height)) + 80
//frame is the frame of the textview toolbarframe of the toolbar and textframe of the view
frame.origin.y = self.messageText.frame.origin.y - rect.height
toolbarFrame.origin.y = self.toolbar.frame.origin.y - rect.height
textFrame.origin.y = self.textfieldFrame.frame.origin.y - rect.height
UIView.animateWithDuration(0.3, animations: {
self.messageText.frame = frame
self.toolbar.frame = toolbarFrame
self.toolbar.multipleTouchEnabled = true
var items = self.toolbar.items
for item in items {
var button :UIBarButtonItem = item as UIBarButtonItem
}
})
UIView.commitAnimations()
}
Can you please explain what I'm doing wrong here?