Jump to a specific subViewController in Swiping - ios

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)

Related

How to add multiple view controllers on scrollview without them overlapping

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

Is it possible to pass a gesture from the current view controller to popover viewcontroller

In my presented ViewController i have a button where on the long press I present a ViewController with modalPresentationStyle popover. The thing is I would like to pass the same touch gesture to the popover so that I don't have to make another touch event to make things work. Is it possible in my current situation or do u suggest work around?
interactionViewController?.delegate = self
interactionViewController?.indexpathRow = indexPathRow
interactionViewController?.modalPresentationStyle = .popover
interactionViewController?.popoverPresentationController?.backgroundColor = .clear
interactionViewController?.preferredContentSize = CGSize(width: 320, height: 220)
interactionViewController?.view.layer.cornerRadius = 25
let popoverMenuViewController = interactionViewController?.popoverPresentationController
popoverMenuViewController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
popoverMenuViewController?.sourceView = sender as? UIView
popoverMenuViewController?.popoverBackgroundViewClass = PopupControllerBackgroundView.self
popoverMenuViewController?.sourceRect = CGRect(x: 0,y:0,width: 1,height: 1)
popoverMenuViewController?.delegate = self
self.present(self.interactionViewController!, animated: false, completion: { [weak self] in
self?.interactionViewController?.view.superview?.layer.cornerRadius = 40
self?.interactionViewController?.view.superview?.backgroundColor = .clear
})

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....

Distance between screen border and bar button item

Is there a way to get the the distance between the outer screen border and the bar button item. I was thinking of something similar like how to get the status bar height?
(I am asking this, because it is different depending on the device.)
This is some trial code:
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, view.frame.size.width, 64))
let navigationBar.backgroundColor = UIColor.redColor()
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil)
navigationItem.leftBarButtonItem = barButtonItem
navigationBar.items = [navigationItem]
let buttonItemView = barButtonItem.valueForKey("view") as! UIView
let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: UIApplication.sharedApplication().keyWindow?.rootViewController?.view)
let xCoordinateMin = CGRectGetMinX(frame)
let yCoordinateMax = CGRectGetMaxY(frame)
let yCoordinateMin = CGRectGetMinY(frame)
let label = UILabel(frame: CGRectMake(CGFloat(xCoordinateMin), CGFloat(yCoordinateMin), 100, yCoordinateMax - yCoordinateMin))
label.backgroundColor = UIColor.greenColor()
But it still gives me 0 as xMin, although it seems to work with the y-coordinate..
By outer screen, I assume you are talking about the main window.
You can try converting the bar button's frame into window's coordinate in order to find the distance to each direction:
let buttonItemView = barButtonItem.valueForKey("view") as! UIView
let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: nil)
print("Distance to left: ", CGRectGetMinX(frame))
print("Distance to top: ", CGRectGetMinY(frame))
However, main window receives rotation events and passes them onto controllers which means it doesn't change its frame size so the above solution will not work properly for landscape mode.
You can try converting button's rect to the root view controller's coordinate system, but now you have to take the status bar height into consideration:
let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: UIApplication.sharedApplication().keyWindow?.rootViewController?.view)
Solved with this code:
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, view.frame.size.width, 124))
navigationBar.backgroundColor = UIColor.redColor()
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil)
navigationItem.leftBarButtonItem = barButtonItem
navigationBar.items = [navigationItem]
let buttonItemView = barButtonItem.valueForKey("view") as! UIView
let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: nil)
let xCoordinateMin: CGFloat = CGRectGetMinX(frame)
let xCoordinateMax: CGFloat = CGRectGetMaxX(frame)
let yCoordinateMax: CGFloat = CGRectGetMaxY(frame)
let yCoordinateMin = CGRectGetMinY(frame)
let label = UILabel(frame: CGRectMake(abs(xCoordinateMin), yCoordinateMin, xCoordinateMax - xCoordinateMin, yCoordinateMax - yCoordinateMin))
label.backgroundColor = UIColor.greenColor()
The trick was to declare the let xCoordinateMin as CGFloat because then it was considered as negative value. Then when creating the label just use abs() to get the absolute value. You now have a label that is absolutely the same size as your bar button item!
UIApplication.sharedApplication().statusBarFrame.height

Keyboard event making toolbar items unclickable

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?

Resources