How to hide NavigationBar from sub view? - swift

I am facing an issue of NavigationBar, I dont want it in subview (child view), I also used setNavigationBarHidden() method to hide but it is not working.
class VehicleSavingPopupViewController: UIViewController {
#IBOutlet weak var bottomView: UIView!
#IBOutlet weak var backGroundView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
animateView()
backGroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backGroundViewTapped(_:))))
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
}
private func animateView(){
UIView.animate(withDuration: 0.5, delay: 0, options: [.transitionCurlDown],
animations: { [weak self] in
guard let self = `self` else {return}
self.bottomView.center.y -= self.bottomView.bounds.height
}, completion: nil)
}

I set this in my own navigationController class but it should work in your viewDidLoad() method as well.
navigationController?.navigationBar.isHidden = true

Related

swift how to update first controller from logic in second one vai protocol and delegate pattern?

I have a label in first view controller ViewController, and a func getting date avery second in second vc. I'd like to update label in first after timer starts in second. is it good to use protocol-delegate pattern? at this moment it is not working, time is going but not updating the view in first VC
my struct for protocol
protocol ViewControllerDelegate: class {
func changeLabelText(textToPass: String)
}
in first viewController
class ViewController: UIViewController, ViewControllerDelegate {
#IBOutlet weak var mainLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func changeLabelText(textToPass: String) {
self.mainLabel.text = textToPass
self.view.layoutIfNeeded()
}
#IBAction func buttonTapped(_ sender: UIButton) {
let nextVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
nextVC.delegateSubscriber = self
present(nextVC, animated: true, completion: nil)
}
}
in secondVC
class SecondViewController: UIViewController {
//MARK: speed timer feature 1/3
private weak var timer: Timer?
private var timerDispatchSourceTimer : DispatchSourceTimer?
weak var delegateSubscriber : ViewControllerDelegate?
#IBOutlet weak var myTxtField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
startTimer(every: 1)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("appeared")
stopTimer()
}
private func startTimer(every timeInterval: TimeInterval) {
if #available(iOS 10.0, *) {
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true) { [weak self] _ in
let dateToPass = Date().description
print(dateToPass)
self?.delegateSubscriber?.changeLabelText(textToPass: dateToPass)
}
}
}
//MARK: speed timer feature 3/3
private func stopTimer() {
timer?.invalidate()
//timerDispatchSourceTimer?.suspend() // if you want to suspend timer
timerDispatchSourceTimer?.cancel()
}
#IBAction func buttonTapped(_ sender: UIButton) {
// delegateSubscriber?.changeLabelText(textToPass: self.myTxtField.text ?? "error")
dismiss(animated: true, completion: nil)
}
}
Just remove [weak self] from Timer closure
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
let dateToPass = Date().description
print(dateToPass)
self.delegateSubscriber?.changeLabelText(textToPass: dateToPass)
}
... then self isn't optional

Swift delegate beetween two VC without segue

I have 3 classes:
ChatLogControoller
GetImageFromLibraty(NSObject class)
ImagePreviewViewController
I want to press a clip from the first VC, then open the media library to pick an image. Then the selected image is passed to the third VC as a previewController. Then if I select 'done' I want to pass it to the first VC.
1st VC
class ChatLogControoller: UICollectionViewController, UICollectionViewDelegateFlowLayout, NSFetchedResultsControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, DataSentDelegate {
func recievePhoto(data: UIImage) {
imageFromView = data
print("-------\(imageFromView = data)")
}
override func viewDidLoad() {
super.viewDidLoad()
let vc = ImagePreviewController()
self.vc.delegate = self
}
2nd class its just picker of image, so i pass image to 3rd VC and this image appears on imageView of 3rd VC successfully!
my 3rd VC
protocol DataSentDelegate {
func recievePhoto(data: UIImage)
}
class PreviewController: UIViewController, UIScrollViewDelegate {
var delegate : DataSentDelegate? = nil
var aImageView: UIImageView!
var aImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(actionSend))
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(actionBack))
}
#objc func actionBack() {
dismiss(animated: false, completion: nil)
}
#objc func actionSend() {
let data = aImageView.image
delegate?.recievePhoto(data: data!)
dismiss(animated: true, completion: nil)
}
You need to create one more protocol in your SecondViewController to Pass that delegate from ThirdViewController to FirstViewController.
FirstViewController:
import UIKit
class ViewController: UIViewController, DataSentDelegate, dataSentDelegate {
#IBOutlet weak var imagefromThirdVC: UIImageView!
var thirdVCImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonTapped(_ sender: Any) {
let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
func goToThirdVC() {
let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
func recievePhoto(data: UIImage) {
thirdVCImage = data
imagefromThirdVC.image = thirdVCImage
}
}
SecondViewController:
import UIKit
protocol dataSentDelegate {
func goToThirdVC()
}
class ViewController2: UIViewController {
#IBOutlet weak var passingImage: UIImageView!
var delegate: dataSentDelegate? = nil
var images: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
images = UIImage(named: "screen")
}
#IBAction func actionButton(_ sender: Any) {
self.delegate?.goToThirdVC()
}
}
ThirdViewController:
import UIKit
protocol DataSentDelegate {
func recievePhoto(data: UIImage)
}
class ViewController3: UIViewController {
var delegate: DataSentDelegate? = nil
#IBOutlet weak var passedImageView: UIImageView!
var passedImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
passedImage = UIImage(named: "screen")
passedImageView.image = passedImage
}
#IBAction func action(_ sender: Any) {
let data = passedImageView.image
delegate?.recievePhoto(data: data!)
// delegate?.goToFirstVC()
guard let viewControllers = self.navigationController?.viewControllers else {
return
}
for firstViewController in viewControllers {
if firstViewController is ViewController {
self.navigationController?.popToViewController(firstViewController, animated: true)
break
}
}
}
}

how to get a textview to autoscroll down when theres a new line

in swift 4.0 for cocoa applications
how do you get a textview to autoscroll down when theres a new line
of text added into it?
is there a built in function for this?
i can't seem to find a way to do this.
chatplace.scroll(<#T##point: NSPoint##NSPoint#>)
Here's a simple example that works:
import UIKit
class ViewController: UIViewController {
var count = 0
var timer:Timer?
#IBOutlet weak var textView: UITextView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
textView.isScrollEnabled = true
textView.becomeFirstResponder()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
timer = Timer(timeInterval: 0.125, repeats: true, block: { (timer) in
self.count += 1
self.textView.text.append("\n\(self.count) This is another line")
})
if let timer = timer {
RunLoop.current.add(timer, forMode: .commonModes)
}
}
}
The result:
If you want to scroll programmatically this should do it:
func scrollToEnd(_ someTextView:UITextView) {
let bottom = NSMakeRange(someTextView.text.lengthOfBytes(using: .utf8)-1, 1)
someTextView.scrollRangeToVisible(bottom)
}

ViewController looses animated position after navigation

I want a view to appearing and disappear from the left (depending if it has some useful information for the user).
I want to view to be positioned using constraints, so I need it to be created from the storyboard.
In this code snippet, the view should be moved out of the way when to code appears.
But: When I segue to the next VC I can see that the view appears again at its original position and when going back from the VC to the initial VC it has, in fact, resumed the original position.
I played a little around with saving the "state" of the view in a variable, making it appear/disappear in the various lifecycles of the VC, but nothing really helped.
How is the best way to achieve this?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
var boxIsVisible = false
override func viewDidLoad() {
super.viewDidLoad()
}
var originalX:CGFloat = 0.0
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
originalX = self.myView.frame.origin.x
if boxIsVisible == false {
self.myView.center.x -= 200
}
}
#IBAction func slideInAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.myView.center.x = self.originalX
self.boxIsVisible = true
})
}
#IBAction func slideOutAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.myView.center.x = -200
self.boxIsVisible = false
})
}
}
Update leading constraint instead of the view's position, because you use auto layout.
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
var boxIsVisible = false
#IBOutlet weak var leadingConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if boxIsVisible == false {
leadingConstraint.constant = -myView.frame.width
}
}
#IBAction func slideInAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.leadingConstraint.constant = -50
self.view.layoutIfNeeded()
self.boxIsVisible = true
})
}
#IBAction func slideOutAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.leadingConstraint.constant = -self.myView.frame.width
self.view.layoutIfNeeded()
self.boxIsVisible = false
})
}
}

Passing parameters to a Selector in Swift using UITapGestureRecognizer, UIImageView and UITableViewCell

I need to identify the image that the user clicked on a TableCell.
How to pass TAG?
class CustomCell: UITableViewCell {
#IBOutlet weak var imgPost1: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
imgPost1.tag=1
let tap = UITapGestureRecognizer(target: self, action: #selector(CustomCell.tappedMe))
imgPost1.addGestureRecognizer(tap)
imgPost1.userInteractionEnabled = true
}
func tappedMe(xTag:Int) {
print(xTag)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
You can use the view property of the UIGestureRecognizer.
Register for tap gesture:
let tap = UITapGestureRecognizer(target: self, action: "tappedMe:")
imgPost1.addGestureRecognizer(tap)
imgPost1.userInteractionEnabled = true
Now define the tappedMe method
func tappedMe(sender: UITapGestureRecognizer) {
print(sender.view?.tag)
}
PS: Don't forget to set the tag for image