In iOS Simulator, I've followed the basic steps to include PencilKit into my view controller. I got the toolKit in the UI but couldn't draw.. Not sure what I'm missing here. Sharing my code which I tried.
import UIKit
import PencilKit
class DrawingBoardViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
func setupCanvasView() {
let toolPicker = PKToolPicker.init()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
toolPicker.isRulerActive = false
canvasView.isOpaque = true
canvasView.becomeFirstResponder()
canvasView.drawingPolicy = .anyInput
}
}
Kindly let me know what I'm missing here..
This should fix your issue:
import UIKit
import PencilKit
class DrawingBoardViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
let toolPicker = PKToolPicker.init()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
func setupCanvasView() {
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
toolPicker.isRulerActive = false
canvasView.isOpaque = true
canvasView.becomeFirstResponder()
canvasView.drawingPolicy = .anyInput
}
}
Related
When I try to change color or tool nothing happens and it still drawing with deafult black pen.
class ViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
private func setupCanvasView() {
canvasView.drawingPolicy = .anyInput
let toolPicker = PKToolPicker()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
}
Can't fix this problem. What should I do?
So I am trying to display a splitView with a tabBar but the NavigationView does not stretch all the way to the bottom. Is there something I am missing?
here is the code I am using:
import Foundation
import SwiftUI
protocol SettingsCoordinatorInput: ManagedCoordinator {
}
class SettingsCoordinator: ManagedCoordinator, SettingsCoordinatorInput {
var navigationController = UINavigationController()
weak var viewModel: SettingsViewModel?
lazy var viewController: UIHostingController<SettingsSplitView> = {
let viewModel = SettingsViewModel(coordinator: self)
self.viewModel = viewModel
return UIHostingController(rootView: SettingsSplitView())
}()
override func topController() -> UIViewController? {
return viewController
}
convenience init(delegate: ManagedCoordinator) {
self.init()
self.delegate = delegate
}
override func start() {
navigationController.viewControllers = [viewController]
}
#objc private func close() {
finish()
}
override func finish() {
super.finish()
viewController.navigationController?.dismiss(animated: true, completion: nil)
}
}
and the result is like this:
I find a lot of resources on how to hide the navigation bar on scroll, but I would like to have the navigation bar hidden on start, and then appear when starting to scroll. Like this animation from Design+Code app: https://imgur.com/a/SqDRD
You can use UIScrollViewDelegate for that.
Here is example code for hide navigation bar and tool bar with scroll:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var toolBar: UIToolbar!
#IBOutlet weak var webV: UIWebView!
var lastOffsetY :CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
webV.scrollView.delegate = self
let url = "http://apple.com"
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webV.loadRequest(request)
}
//Delegate Methods
func scrollViewWillBeginDragging(scrollView: UIScrollView){
lastOffsetY = scrollView.contentOffset.y
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView){
let hide = scrollView.contentOffset.y > self.lastOffsetY
self.navigationController?.setNavigationBarHidden(hide, animated: true)
toolBar.hidden = hide
}
}
Using the willBeginDragging and didEndDragging you can accomplish what you want. Here there is a simplified version of it, you may need to modify it a little bit to obtain the desired effect you want, but it is an starting point.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
navigationController?.setNavigationBarHidden(false, animated: true)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
navigationController?.setNavigationBarHidden(true, animated: true)
}
}
I'm having trouble getting the following code to compile, any suggestions people?
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var avatar: UIImageView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var contentImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
avatar.layer.cornerRadius = 5.0
avatar.layer.borderWidth = 4.0
avatar.layer.borderColor = UIColor.whiteColor().CGColor
avatar.clipsToBounds = true
scrollView.delegate = self
contentImageView.clipsToBounds = true
}
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let yOffset = self.scrollView.contentOffset.y * 0.2
let availableOffset = min(yOffset, 60)
let contentRectYOffset = availableOffset / contentImageView.frame.size.height
contentImageView.layer.contentsRect = CGRectMake(0.0, contentRectYOffset, 1, 1);
}
}
The error is
'Downcast from 'UIViewController?' to 'UIViewController' only unwraps
optionals; did you mean to use '!'
This happens in the line
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
You are force down casting an object which is already of same type. You just need to remove as! UIViewController and assuming the ViewController with identifier LoginScreen exists, you can force unwrap the vc variable when needed.
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen")
self.presentViewController(vc!, animated: true, completion: nil)
}
Thanks R P, your solution worked a treat.
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen")
self.presentViewController(vc!, animated: true, completion: nil)
}
As a relative noob to iOS development I'm struggling to make a PHFComposeBarView control become active in my Swift iOS app.
Having squinted at the Obj-C in the PHFComposeBarView example app and this SO answer, I've managed as best I can to translate the basic setup into Swift such that the control is displayed in the main view:
However, I haven't managed to make the control become active programatically.
I've created a minimal test case project in Swift 2.0/XCode 7.0 Beta 2 whose ViewController looks like:
ViewController.swift:
import UIKit
import PHFComposeBarView
class ViewController: UIViewController, PHFComposeBarViewDelegate {
var composeBar: PHFComposeBarView {
let viewBounds = self.view.bounds
let frame = CGRectMake(0.0, viewBounds.size.height - PHFComposeBarViewInitialHeight, viewBounds.size.width, PHFComposeBarViewInitialHeight)
let composeBarView = PHFComposeBarView(frame: frame)
composeBarView.delegate = self
return composeBarView
}
override var inputAccessoryView: UIView {
return self.composeBar
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func viewDidAppear(animated: Bool) {
print("viewDidAppear self \(self)")
print("composeBar.delegate \(self.composeBar.delegate)")
print("composeBar.textView.delegate \(self.composeBar.textView.delegate)")
print("composeBar.canBecomeFirstResponder() \(self.composeBar.canBecomeFirstResponder())")
print("composeBar.textView.canBecomeFirstResponder() \(self.composeBar.textView.canBecomeFirstResponder())")
let res = self.composeBar.becomeFirstResponder()
print("composeBar.becomeFirstResponder() \(res)")
print("composeBar.isFirstResponder() \(self.composeBar.isFirstResponder())")
print("composeBar.textView.isFirstResponder() \(self.composeBar.textView.isFirstResponder())")
}
}
Console:
viewDidAppear self <SwiftPHFComposeBarTest.ViewController: 0x7fe088619d90>
composeBar.delegate Optional(<SwiftPHFComposeBarTest.ViewController: 0x7fe088619d90>)
composeBar.textView.delegate Optional(<PHFDelegateChain: 0x7fe088654850>)
composeBar.canBecomeFirstResponder() true
composeBar.textView.canBecomeFirstResponder() true
composeBar.becomeFirstResponder() false
composeBar.isFirstResponder() false
composeBar.textView.isFirstResponder() false
How can it be that composeBar.canBecomeFirstResponder() returns true but composeBar.becomeFirstResponder() returns false?
Also, I'm not sure if the delegate being an Optional is a problem.
EDIT: Updated to include debug output.
Well, one solution appears to be to use the storyboard instead:
ViewController.swift:
import UIKit
import PHFComposeBarView
class ViewController: UIViewController, PHFComposeBarViewDelegate {
#IBOutlet weak var composeBarView: PHFComposeBarView!
override func viewDidLoad() {
super.viewDidLoad()
composeBarView.delegate = self
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var inputAccessoryView: UIView {
composeBarView.removeFromSuperview()
return composeBarView
}
}