I copied this code for the sliding view controller from a YouTube snapchat like menu video but it won't compile.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var V1 : View1 = View1(nibName: "View1" , bundle: nil)
self.addChildViewController(V1)
self.scrollView.addSubview(V1.view)
V1.didMoveToParentViewController(self)
}
I keep getting the error use of undeclared type 'View1'. How do I fix this? I followed a tutorial and the code is identical. How do I declare View1 as a type?
I've made the same tutorial but for three Views:
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let view1: View1 = View1(nibName: "View1", bundle: nil)
addChildViewController(view1)
scrollView.addSubview(view1.view)
view1.didMoveToParentViewController(self)
let view2: View2 = View2(nibName: "View2", bundle: nil)
addChildViewController(view2)
scrollView.addSubview(view2.view)
view2.didMoveToParentViewController(self)
var view2Frame: CGRect = view2.view.frame
view2Frame.origin.x = view.frame.width
view2.view.frame = view2Frame
let view3: View3 = View3(nibName: "View3", bundle: nil)
addChildViewController(view3)
scrollView.addSubview(view3.view)
view3.didMoveToParentViewController(self)
var view3Frame: CGRect = view3.view.frame
view3Frame.origin.x = view.frame.width * 2
view3.view.frame = view3Frame
self.scrollView.contentSize.width = view.frame.width * 3
}
And this works just fine. Hope it helps.
Related
Why am I getting a breakpoint 2.5 on my outlet named scrollPages? I'm using it to make let the user scroll up and down but for some reason I'm getting breakpoint 2.5.
I didn't get the breakpoint before when I tried the app, then I added a feature that didn't work then I it and now when I removed the feature I still get it.
Here is my code and the outlet that gives me the breakpoint has a --> next to it. Thanks!
// ViewController.swift
import UIKit
class ViewController: UIViewController, UIApplicationDelegate {
--> #IBOutlet weak var scrollPages: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let V1 : View1 = View1(nibName: "View1", bundle: nil)
let V2 : View1 = View1(nibName: "View2", bundle: nil)
self.addChildViewController(V1)
self.scrollPages.addSubview(V1.view)
V1.didMove(toParentViewController: self)
self.addChildViewController(V2)
self.scrollPages.addSubview(V2.view)
V1.didMove(toParentViewController: self)
var V2Frame : CGRect = V2.view.frame
V2Frame.origin.y = self.view.frame.height
V2.view.frame = V2Frame
self.scrollPages.contentSize = CGSize(width: self.view.frame.width,
height: self.view.frame.height * 2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is Alibaba.com's ios application. In the app, if I swipe left the bar will scroll left and reveal more clickable options. My question is, how is this made?
Thanks!
I solved it.
1. Firstly make a .xib file, name it to whatever you want, I named mine to unnamedView, then add the labels and imageViews.
2. Then create UnnamedView class file like this and add all labels and images
class unnamedView: UIView {
#IBOutlet weak var firstButton: UIButton!
#IBOutlet weak var firstImage: UIImageView!
#IBOutlet weak var firstLabel: UILabel!
}
3. Then create a array for the UnnamedView in the main view controller.
let unnamed = ["title1":"textTitleToLabel","image1":"nameOfImage"]
var unnamedArray = [Dictionary<String,String>]()
4. Then I added the scroll view to the main view controller
#IBOutlet weak var unnamedScrollView: UIScrollView!
5. Inside viewDidLoad() add following
unnamedArray = [unnamed]
unnamedScrollView.isPagingEnabled = true
unnamedScrollView.contentSize = CGSize(width: unnamedScrollView.bounds.width * CGFloat(unnamedArray.count), height: 100)
unnamedScrollView.showsHorizontalScrollIndicator = false
unnamedScrollView.delegate = self
loadUnnamed()
6. And lastly added following bellow the viewDidLoad
func loadCategories() {
for (index, category) in categoriesArray.enumerated() {
if let unnamedView = Bundle.main.loadNibNamed("unnamedView", owner: self, options: nil)?.first as? unnamedView {
unnamedView.firstImage.image = UIImage(named: category["image1"]!)
unnamedView.firstLabel.text = category["title1"]
unnamedView.firstButton.tag = index
unnamedView.firstButton.addTarget(self, action: #selector(ViewController.gotoCategory(sender:)), for: .touchUpInside)
unnamedScrollView.addSubview(unnamedView)
unnamedView.frame.size.width = self.view.bounds.size.width
unnamedView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
}
}
}
func gotoCategory (sender:UIButton) {
print("The user wants to buy feature \(sender.tag)")
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "tableView")
self.present(newViewController, animated: true, completion: nil)
}
And also, if you'd like to add page control dots, simply to this.
#IBOutlet weak var featurePageControl: UIPageControl!
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
featurePageControl.currentPage = Int(page)
}
I have a question to use UITableView in xib file attaching to UIScrollView with checked paging enabled.
I am not native speaker in english, so please see this carefully.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var menuScrollView: UIScrollView!
#IBOutlet var pageScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let V1: View1 = View1(nibName: "View1", bundle: nil)
let V2: View2 = View2(nibName: "View2", bundle: nil)
self.addChildViewController(V1)
self.pageScrollView.addSubview(V1.view)
V1.didMoveToParentViewController(self)
self.addChildViewController(V2)
self.pageScrollView.addSubview(V2.view)
V2.didMoveToParentViewController(self)
var V2Frame: CGRect = V2.view.frame
V2Frame.origin.x = self.view.frame.width
V2.view.frame = V2Frame
print("view width:",self.view.frame.width, "v2 origin x:",V2.view.frame.origin.x)
self.pageScrollView.contentSize = CGSizeMake(self.view.frame.width * 2, self.pageScrollView.frame.height)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The first page shows the little bit of second page but I coded ,as shown as above code,
V2Frame.origin.x = self.view.frame.width
I thought this would work to fit width of the view but it doesn't.
I want the View1 and View2 to fit the UIScrollView.
I really appreciate to give me a help.
I have three step view controllers. I'm trying to display those three steps on a single viewcontroller (RegisterViewController) using a scroll view so that users can swipe through the steps.
I saw some other posts and got this far. When I run the app I'm getting a blank screen with the background color of the RegisterViewController. I wrote some println statements in the step view controllers to check whether they are being instantiated or not and I could see those statements' output on console. Please if anybody can help?
class RegisterViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var registerStepsPageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
setupScrollView()
}
func setupScrollView() {
let storyboard = UIStoryboard(name: "Login", bundle: nil)
let stepOneViewController = storyboard.instantiateViewControllerWithIdentifier("RegisterStepOne") as! RegisterStepOneViewController
let stepTwoViewController = storyboard.instantiateViewControllerWithIdentifier("RegisterStepTwo") as! RegisterStepTwoViewController
let stepThreeViewController = storyboard.instantiateViewControllerWithIdentifier("RegisterStepThree") as! RegisterStepThreeViewController
var stepViewControllers = [stepOneViewController, stepTwoViewController, stepThreeViewController]
for index in 0..<stepViewControllers.count {
self.addChildViewController(stepViewControllers[index])
let originX: CGFloat = CGFloat(index) * CGRectGetWidth(self.scrollView.frame);
stepViewControllers[index].view.frame = CGRectMake(originX, 0, self.view.frame.size.width, self.view.frame.size.height);
stepViewControllers[index].view.frame.size = self.view.frame.size
self.scrollView.addSubview(stepViewControllers[index].view)
stepViewControllers[index].didMoveToParentViewController(self)
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(stepViewControllers.count), self.view.frame.height)
self.scrollView.delegate = self
self.registerStepsPageControl.numberOfPages = stepViewControllers.count
}
}
EDIT01:
This code below works. It loads the images and they are being displayed as they are supposed to. But I don't know what might be the problem while loading views from other viewcontrollers.
#IBOutlet weak var tutorialStepsPageControl: UIPageControl!
#IBOutlet weak var scrollView: UIScrollView!
var stepImages: [UIImage] = [UIImage(named: "tutorial_image_step1")!, UIImage(named: "tutorial_image_step2")!, UIImage(named: "tutorial_image_step3")!]
override func viewDidLoad() {
super.viewDidLoad()
self.tutorialStepsPageControl.numberOfPages = stepImages.count
setupScrollView()
}
func setupScrollView() {
var imageFrame: CGRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
var imageView: UIImageView!
for index in 0..<stepImages.count {
imageFrame.origin.x = self.view.frame.size.width * CGFloat(index)
imageFrame.size = self.view.frame.size
imageView = UIImageView(frame: imageFrame)
imageView.image = stepImages[index]
imageView.contentMode = .ScaleAspectFit
self.scrollView.addSubview(imageView)
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(stepImages.count), self.view.frame.size.height)
self.scrollView.delegate = self
}
Try moving the line:
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * CGFloat(stepViewControllers.count), self.view.frame.height)
Above this line:
for index in 0..<stepViewControllers.count {
This issue might be coming from the fact that you're adding the subviews before you sent the contentSize.
Okay I deleted the step controllers and added them again from scratch. Did a clean build and now it's working. Don't know what was I doing wrong. :/
I'm trying to get my keybordview to switch on orientation change - I have my views within my .xib hooked up with #IBOutlet then I call them with loadInterface() inside of viewDidLoad() I was hoping that my call in viewDidLoad would update the interface but I don't think it's working quite how I thought - it doesn't break anything but it doesn't work either (though, I've made tons of progress from last week.).
I have a feeling it's something with the orientation change...
import UIKit
class KeyboardViewController: UIInputViewController {
#IBOutlet var keyboardViewPortrait: UIView!
#IBOutlet var keyboardViewLandscape: UIView!
func loadInterface(){
var keyboardNib = UINib(nibName: "keyboardView", bundle: nil)
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
if self.interfaceOrientation.isLandscape {
self.keyboardViewLandscape = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
view.addSubview(self.keyboardViewLandscape)
view.backgroundColor = self.keyboardViewLandscape.backgroundColor
}
else if self.interfaceOrientation.isPortrait {
self.keyboardViewPortrait = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
view.addSubview(self.keyboardViewPortrait)
view.backgroundColor = self.keyboardViewPortrait.backgroundColor
}
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
}
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
loadInterface()
...
}
}
*update
I think rdelmar set me on the right path but something is still missing.
thanks!
The problem is that your assigning the same view to self.keyboardViewLandscape, and self.keyboardViewPortrait (in both cases that's the view you get from keyboardNib.instantiateWithOwner(self, options: nil)[0]). One of those properties should be assigned to the object at index 1. Also, it might be better to use the new method for dealing with rotation, viewWillTransitionToSize:withTransitionCoordinator:. You also need to remove the previous view when you add the new one.
class ViewController: UIViewController {
var keyboardViewPortrait: UIView!
var keyboardViewLandscape: UIView!
var keyboardNib = UINib(nibName: "keyboardView", bundle: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.keyboardViewPortrait = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
view.addSubview(self.keyboardViewPortrait)
view.backgroundColor = self.keyboardViewPortrait.backgroundColor
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator){
if size.width > size.height {
self.keyboardViewPortrait.removeFromSuperview()
self.keyboardViewLandscape = keyboardNib.instantiateWithOwner(self, options: nil)[1] as UIView
view.addSubview(self.keyboardViewLandscape)
view.backgroundColor = self.keyboardViewLandscape.backgroundColor
}
else {
self.keyboardViewLandscape.removeFromSuperview()
self.keyboardViewPortrait = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView
view.addSubview(self.keyboardViewPortrait)
view.backgroundColor = self.keyboardViewPortrait.backgroundColor
}
}
}