I'm trying to make a view container with table view. That's works fine, but if i select the last row, the method didSelectRowAt indexPath: cannot be triggered. This is my code:
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let controller = storyboard.instantiateViewController(withIdentifier: "pageViewController") as! PageViewController
addChildViewController(controller)
controller.customDelegate = self
controller.index = index
self.controller = controller
//Add Controllers
let clientInfoVC = storyboard.instantiateViewController(withIdentifier: "ClientInfo") as! ClientInfoViewController
clientInfoVC.client = self.client
let clientRequestsVC = storyboard.instantiateViewController(withIdentifier: "ClientRequests") as! ClientProductRequestViewController
clientRequestsVC.client = self.client
let clientDebtsVC = storyboard.instantiateViewController(withIdentifier: "ClientDebts") as! ClientDebtViewController
clientDebtsVC.client = self.client
controller.orderedViewControllers = [clientInfoVC,clientRequestsVC,clientDebtsVC]
controller.view.frame = CGRect(x: 0, y: 0, width: self.clientContainerView.frame.width, height: self.clientContainerView.frame.height)
self.clientContainerView.addSubview(controller.view)
controller.didMove(toParentViewController: self)
And this is my container view:
What am I doing wrong?
Please help me
I found the solution:
I needed to remove the controller's frame setting and thats it. So Just remove this line:
controller.view.frame = CGRect(x: 0, y: 0, width: self.clientContainerView.frame.width,
height: self.clientContainerView.frame.height)
Related
I want to add three views to a scrollview. These views have to be next to one another like Snapchat. However, when I launch my app on another device which is another size, the views don't adapt. Does someone know how can I resolve this problem?
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let MainEuroView = storyboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let TicketEuroView = storyboard.instantiateViewController(withIdentifier: "TicketViewController") as! TicketViewController
let ProfilView = storyboard.instantiateViewController(withIdentifier: "ProfilViewController") as! ProfilViewController
var TicketViewFrame : CGRect = TicketView.view.frame
TicketViewFrame.origin.x = 0
TicketView.view.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.size.height)
TicketView.view.frame = TicketViewFrame
var MainViewFrame : CGRect = MainView.view.frame
MainViewFrame.origin.x = self.view.frame.width
MainView.view.frame = MainViewFrame
var ProfilViewFrame : CGRect = ProfilView.view.frame
ProfilViewFrame.origin.x = 2 * self.view.frame.width
ProfilView.view.frame = ProfilViewFrame
self.addChildViewController(TicketView)
self.main_Scroll_View.addSubview(TicketView.view)
TicketView.didMove(toParentViewController: self)
self.addChildViewController(MainView)
self.main_Scroll_View.addSubview(MainView.view)
MainView.didMove(toParentViewController: self)
self.addChildViewController(ProfilView)
self.main_Scroll_View.addSubview(ProfilView.view)
ProfilView.didMove(toParentViewController: self)
self.main_Scroll_View.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)
A quick fix would be to move the frame adjustment to viewDidLayoutSubviews method, you usually don't have the view frame during viewDidLoad, but instead get the frame of the view in the storyboard, e.g.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var TicketViewFrame : CGRect = TicketView.view.frame
TicketViewFrame.origin.x = 0
TicketView.view.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.size.height)
TicketView.view.frame = TicketViewFrame
var MainViewFrame : CGRect = MainView.view.frame
MainViewFrame.origin.x = self.view.frame.width
MainView.view.frame = MainViewFrame
var ProfilViewFrame : CGRect = ProfilView.view.frame
ProfilViewFrame.origin.x = 2 * self.view.frame.width
ProfilView.view.frame = ProfilViewFrame
}
A better fix would be to implement autolayout which will do this automatically for you.
I am having baseview controller with scrollview.In that scrollview I adding three viewcontrollers view(xib).So i can scroll those three in horizantal.In my first view controller I am displaying a tablevieew.While running my app,Its displaying all my 3 view controllers.But When I touch my tableview cell are disappeared.Same I have did in xcode 7.3.It was working fine.But in xcode 8 its not working.Please help me to solve this.I have attached my sample code
let scrollViewWidth:CGFloat = self.scrollContainer.frame.width
let scrollViewHeight:CGFloat = self.scrollContainer.frame.height
let x = CGFloat(i) * scrollViewWidth
if i == 0{
let qualification = EducationViewController (nibName: "EducationViewController", bundle: nil)
//qualification.view.frame.size.height = scrollViewHeight
//qualification.view.frame.size.width = scrollViewWidth
qualification.view.frame.origin.x = x
self.scrollContainer!.addSubview(qualification.view)
qualification.didMove(toParentViewController: self)
}
else if i == 1{
let state = StateRegistrationViewController (nibName: "StateRegistrationViewController", bundle: nil)
//state.view.frame.size.height = scrollViewHeight
// state.view.frame.size.width = scrollViewWidth
state.view.frame.origin.x = x
self.scrollContainer!.addSubview(state.view)
state.view.backgroundColor = UIColor.red
state.didMove(toParentViewController: self)
}
else if i == 2{
let exp = ExperienceViewController (nibName: "ExperienceViewController", bundle: nil)
// exp.view.frame.size.height = scrollViewHeight
// exp.view.frame.size.width = scrollViewWidth
exp.view.frame.origin.x = x
self.scrollContainer!.addSubview(exp.view)
exp.view.backgroundColor = UIColor.orange
exp.didMove(toParentViewController: self)
}}
Initiate the viewController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let aViewController = storyboard.instantiateViewController(withIdentifier: "A") as! AViewController;
let bViewController = storyboard.instantiateViewController(withIdentifier: "B") as! BViewController;
let cViewController = storyboard.instantiateViewController(withIdentifier: "C") as! CViewController;
Add the viewControllers into an array
let viewControllers = [aViewController, bViewController, cViewController]
Run the for loop which adds the viewControllers to the scrollView
var idx:Int = 0
for viewController in viewControllers {
addChildViewController(viewController);
let originX:CGFloat = CGFloat(idx) * width;
viewController.view.frame = CGRect(x: originX, y: 0, width: width, height: height);
scrollView!.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
idx += 1;
}
All Done!
while adding the view of your custom viewcontroller on scrollview you should also add your custom viewcontroller as child controller of the main controller.
like in your case you have three custom controllers then as per the if else clause in your code you must add:
self.addChildViewController(qualification)
self.addChildViewController(state)
self.addChildViewController(exp)
in there respective blocks in your code.
Your code will be like:
let scrollViewWidth:CGFloat = self.scrollContainer.frame.width
let scrollViewHeight:CGFloat = self.scrollContainer.frame.height
let x = CGFloat(i) * scrollViewWidth
if i == 0{
let qualification = EducationViewController (nibName: "EducationViewController", bundle: nil)
//qualification.view.frame.size.height = scrollViewHeight
//qualification.view.frame.size.width = scrollViewWidth
qualification.view.frame.origin.x = x
self.scrollContainer!.addSubview(qualification.view)
self.addChildViewController(qualification)
qualification.didMove(toParentViewController: self)
}
else if i == 1{
let state = StateRegistrationViewController (nibName: "StateRegistrationViewController", bundle: nil)
//state.view.frame.size.height = scrollViewHeight
// state.view.frame.size.width = scrollViewWidth
state.view.frame.origin.x = x
self.scrollContainer!.addSubview(state.view)
self.addChildViewController(state)
state.view.backgroundColor = UIColor.red
state.didMove(toParentViewController: self)
}
else if i == 2{
let exp = ExperienceViewController (nibName: "ExperienceViewController", bundle: nil)
// exp.view.frame.size.height = scrollViewHeight
// exp.view.frame.size.width = scrollViewWidth
exp.view.frame.origin.x = x
self.scrollContainer!.addSubview(exp.view)
self.addChildViewController(exp)
exp.view.backgroundColor = UIColor.orange
exp.didMove(toParentViewController: self)
}}
I'm trying to present a popover from a UIMenuItem which anchor point is the rect of a selected text in a textView. I have the following code:
func pickColor(sender: UIMenuItem) {
let range = noteTextView.selectedTextRange
let beginningOfSelection = noteTextView.caretRect(for: (range?.start)!)
let endOfSelection = noteTextView.caretRect(for: (range?.end)!)
let storyboard: UIStoryboard = UIStoryboard(name: "ColorPicker", bundle: nil)
let colorVC = storyboard.instantiateViewController(withIdentifier: "ColorPickerViewController") as UIViewController
colorVC.modalPresentationStyle = .popover
let popover: UIPopoverPresentationController = colorVC.popoverPresentationController!
popover.sourceView = noteTextView
popover.sourceRect = CGRect(x: (beginningOfSelection.origin.x + endOfSelection.origin.x)/2, y: (beginningOfSelection.origin.y + beginningOfSelection.size.height)/2, width: 0, height: 0)
present(colorVC, animated: true, completion: nil)
}
The app crashes in the line colorVC.modalPresentationStyle = .popover. Can someone tell me what's going on here? Thanks! :)
I want to show UITableViewController as popup on button press. I am able to show popup using this code
let menuViewController = popTableViewController() //popTableViewController
menuViewController.modalPresentationStyle = .Popover
menuViewController.preferredContentSize = CGSizeMake(320, 132)
//menuViewController.tableView = FrontTable
let popoverMenuViewController = menuViewController.popoverPresentationController
popoverMenuViewController?.permittedArrowDirections = .Any
popoverMenuViewController?.delegate = self
popoverMenuViewController?.sourceView = sender
popoverMenuViewController?.sourceRect = CGRect(
x: 10,
y: 10,
width: 1,
height: 1)
presentViewController(
menuViewController,
animated: true,
completion: nil)
but it is showing blank table view in popup. I have also added the content in tableview class but it not showing any reflection on result.
Try adding below code in your UIViewController
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
or you can try below code to create popover
let mpopover = self.storyboard?.instantiateViewControllerWithIdentifier("breedPop") as! breedPopover
mpopover.delegate = self
self.citiesPopover = mpopover
citiesPopover!.modalPresentationStyle = .Popover
citiesPopover!.preferredContentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height/2)
let popoverPresentationViewController = citiesPopover!.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = UIPopoverArrowDirection.Any
popoverPresentationViewController?.delegate = self
popoverPresentationViewController?.sourceView = breedTextField
popoverPresentationViewController?.sourceRect = CGRectMake(breedTextField.frame.width, breedTextField.frame.height*3, 0, 0)
presentViewController(citiesPopover!, animated: true, completion: nil)
The Code I Have written for loading view controllers in Page View Controllers.
Programatically creating four view controllers and adding them to Page View Controller.
I have changed the view controllers frame position but still not changing in the app
let controller: UIViewController = UIViewController()
print(controller.view.frame)
controller.view.frame = CGRectMake(10, 20, self.view.frame.width/2, self.view.frame.height - 20)
print(controller.view.frame)
controller.view.backgroundColor = UIColor.blackColor()
let controller2: UIViewController = UIViewController()
controller2.view.backgroundColor = UIColor.redColor()
let controller3: UIViewController = UIViewController()
controller3.view.backgroundColor = UIColor.blackColor()
let controller4: UIViewController = UIViewController()
controller4.view.backgroundColor = UIColor.greenColor()
let p1 = controller
let p2 = controller2
let p3 = controller3
let p4 = controller4
myViewControllers = [p1,p2,p3,p4]
for index in 0 ..< myViewControllers.count {
NSLog("\(myViewControllers[index])")
}
let startingViewController = self.viewControllerAtIndex(0)
let viewControllers: NSArray = [startingViewController]
self.setViewControllers(viewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: {(done: Bool) in
})