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
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
})
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....
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
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?