How to add a UIView which is created in storyboard as a pop-up in Swift? - iphone

I did the following:
1. Dragged UIView to place it in the ViewController ribbon
Created a custom UIView class and added the outlets to the 'Edit Profile View' view fields:
import UIKit
class EditProfileView: UIView {
let globalDataHandler = GlobalDataHandler.sharedInstance
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var lastNameTextField: UITextField!
#IBOutlet weak var userNameTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
#IBAction func saveEditButton(sender: AnyObject) {
}
#IBAction func cancelEditButton(sender: AnyObject) {
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
Gave 'EditProfileView' class as the Custom Class for the 'Edit Profile View' view in Storyboard.
Created an object for 'EditProfileView' class in ProfileViewController and added the 'Edit Profile View' view to the main view upon clicking edit button in ProfileViewController.
class ProfileViewController: UIViewController {
let profileView = EditProfileView()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func editProfileButton(sender: AnyObject) {
profileView.firstNameTextField.text = "First Name"
profileView.lastNameTextField.text = "Last Name"
profileView.userNameTextField.text = "User Name"
profileView.emailTextField.text = "Email"
let testFrame : CGRect = CGRectMake(50,100,300,300)
profileView.frame = testFrame
self.view.addSubview(profileView)
}
}
But, the 'Edit Profile View' view doesn't appear on ProfileViewController.
Please help.

You have to link your UIView and your View (should be a distinct UIViewController) in the Xib.
profileView = storyboard.instantiateViewControllerWithIdentifier("ProfileViewController")
You must have an UIPresentationController for your ProfileVC
class ProfilePresentationController : UIPresentationController {
override func frameOfPresentedViewInContainerView() -> CGRect {
return CGRect(x: (presentingViewController.view.frame.width / 2.0) - 150.0, y: (presentingViewController.view.frame.height / 2.0) - 100.0, width: 300.0, height: 200.0)
}
}
Your EditProfileView must implement the following delegate:
UIViewControllerTransitioningDelegate
the following properties of your profileView must also be set to:
profileView.modalPresentationStyle = UIModalPresentationStyle.Custom
profileView.transitioningDelegate = self
After that you can implement the delegate required method
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
return ProfilePresentationController(presentedViewController: presented, presentingViewController: presenting)
}

Related

How to make a protocol for UIView which performs a segue

I have added a protocol that makes a segue from a UIView button to a NavigationController but when i compile and press the button after the UIView opens it just does nothing which seems pretty weird since it all makes sense to me at least, Please if I am doing anything wrong lead me to fix my code.
I got this code from this Answer: https://stackoverflow.com/a/52789290/18277261
Code:
class popUpView: UIView, UITextFieldDelegate {
#IBOutlet weak var popUpViewInterface: UIView!
#IBOutlet weak var payNowPopUp: UIButton!
let delegate: popUpViewDelegate? = nil
#IBAction func payNowToWebView(_ sender: Any) {
let myColor = UIColor.red
priceTextFieldPopUp.layer.borderColor = myColor.cgColor
priceTextFieldPopUp.layer.borderWidth = 1.0
self.delegate?.performSegueFromView(withIdentifier: "paymentWebView")
}
}
protocol popUpViewDelegate {
func performSegueFromView(withIdentifier identifier: String)
}
class dashboardViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, ChartViewDelegate, AxisValueFormatter, popUpViewDelegate {
func performSegueFromView(withIdentifier identifier: String) {
self.performSegue(withIdentifier: identifier, sender: nil)
}
In dashboardViewController where you initialise popUpView make sure that you set the delegate like below.
let popView = popUpView()
popView.delegate = self

how to customise UINavigation bar with two labels left aligned

Requirement
I am only able to bring the title at centre
how it can be done?
You can create a custom view and set it as the titleView of the navigationItem.
TitleView.xib
Add a UIView and create the layout as you wish.
Set the backgroundColor of the View and ViewController to clear.
TitleView.swift
import UIKit
class TitleView: UIView {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
class func instanceFromNib() -> TitleView {
return UINib(nibName: "TitleView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! TitleView
}
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
}
ViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.titleView = TitleView.instanceFromNib()
}
Preview
Custom view, set navigationItem.titleView.

Using Delegation to Pass Data from the Initial View Controller to a Second View Controller - Swift

I am trying to understand delegation in Swift better, and I have seen how delegation can be used to pass data from a second view controller back to an initial view controller. But I am confused about how to pass data from the initial view to a second view using delegation. I have initialized an instance of the delegate in my initial view and implement the delegate protocol in the second view, but I am still unsuccessful in passing the data from initial view to second view using delegation. Any advice on what I might be doing wrong would be appreciated! Here is the storyboard view of what my delegation attempt looks like. 1
Initial View Class:
import UIKit
class ViewController: UIViewController {
var data: DataDelegate?
#IBOutlet weak var text: UITextField!
#IBOutlet weak var send: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func send(_ sender: Any) {
let msg = text.text!
data?.sendData(data: msg)
print("SENDING MESSAGE: \(msg)")
}
}
protocol DataDelegate {
func sendData(data: String)
}
Second View Class:
import UIKit
class RVC: UIViewController, DataDelegate {
#IBOutlet weak var delegationLabel: UILabel!
#IBOutlet weak var message: UILabel!
#IBOutlet weak var back: UIButton!
let vc = ViewController()
func sendData(data: String) {
print("DATA SENT")
let msg = data
print(msg)
message.text = data
}
override func viewDidLoad() {
super.viewDidLoad()
print("LOADED")
vc.data = self
// Do any additional setup after loading the view.
}
}

Unable to perform segue from UIView connected to XIB

This is UIView file's owner of XIB
import UIKit
class sideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let mapviewcontroller = MapViewController()
#IBAction func userProfile(_ sender: UIButton) {
mapviewcontroller.performSegueFromView(stringFromView: "userprofile")
}
Whereas inside my UIviewcontroller
class MapViewController: UIViewController
{
func performSegueFromView(stringFromView:String){
performSegue(withIdentifier: "\(stringFromView)", sender: nil)
}
}
It says that my segue identifier does not exist, please help, i'm quite lost. And i couldn't perform segue in UIView as well, and i tried to use storyboard ID method too, but the present(vc,animated:true,completion:nil) not showing up.
First things first, I think you should not access the view controller from your view. It is better to model the controller logic in the view controller rather than in the view.
That said, you can use a delegate pattern to implement your functionality.
import UIKit
class SideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let delegate: SideBarContentDelegate? = nil
#IBAction func userProfile(_ sender: UIButton) {
self.delegate?.performSegueFromView(withIdentifier: "userprofile")
}
}
protocol SideBarContentDelegate {
func performSegueFromView(withIdentifier identifier: String)
}
class MapViewController: UIViewController, SideBarContentDelegate {
#IBOutlet private weak var sidebarView: SideBarContent!
override func viewDidLoad() {
super.viewDidLoad()
self.sidebarView.delegate = self
}
func performSegueFromView(withIdentifier identifier: String) {
self.performSegue(withIdentifier: identifier, sender: nil)
}
}
More info : https://stackoverflow.com/a/1340470/2603900

Swift 4: scrollViewDidScroll not working

I created first View controller "MainViewController" and put into it Container View with outlet contentContainer.
I created second View Controller "AboutContentViewController" and put into it ScrollView
I connect Container View and AboutContentViewController programmatically:
class MainViewController: UIViewController {
#IBOutlet weak var contentContainer: UIView!
override func viewDidLoad() {
let contentControllerName = "AboutContentViewController"
if let contentController = storyboard?.instantiateViewController(withIdentifier: contentControllerName) {
contentContainer.addSubview(contentController.view)
}
}
}
I tried to catch scrollViewDidScroll event using this code
class AboutContentViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var topGalleryScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
topGalleryScrollView.contentSize = CGSize(width: 3750.0, height: 417.0)
topGalleryScrollView.delegate = self
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scrollViewDidScroll called")
}
}
In this context scrollViewDidScroll event not working
I have solved problem. AboutContentViewController is need to be add as child view controller to MainVewController before adding its's view to container:
class MainViewController: UIViewController {
#IBOutlet weak var contentContainer: UIView!
override func viewDidLoad() {
let contentControllerName = "AboutContentViewController"
if let contentController = storyboard?.instantiateViewController(withIdentifier: contentControllerName) {
addChildViewController(contentController)
contentContainer.addSubview(contentController.view)
}
}
}