how to customise UINavigation bar with two labels left aligned - swift

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.

Related

How to resize the detailsView after cross button is clicked after the closing view is hidden?

[![when the cross button is clicked, the closingView is hidden.][1]][1]
import UIKit
import IBAnimatable
class ViewController: UIViewController {
#IBOutlet weak var closingView: AnimatableView!
#IBOutlet weak var detailsView: AnimatableView!
#IBOutlet weak var btnClose: UIButton!
#IBOutlet weak var closeViewBtn: UIView!
#IBOutlet weak var scrollView: UIScrollView!
var isCloseButtonTapped = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btnCloseAction(_ sender: Any) {
isCloseButtonTapped = !isCloseButtonTapped
if isCloseButtonTapped{
closingView.isHidden = true
}else{
//
}
}
}
[1]: https://i.stack.imgur.com/CpH2A.png
You can add closingView and detailsView inside a UIStackView. When the closingView is hidden the detailsView will fill up the entire space in the parent UIStackView.

IBOutlet nil for custom view xib that is embedded in other xib

I am adding a subview(xib) to the view controller. The subview contains other subview(xib).
When the app loads, both the subviews are loaded but , the last child subview has all it IBOutlets as nil.
Below is the simple code with Viewcontroller and two views.
The loadUI() method is printing nil.
Heres the full code. https://github.com/Laxmanraju/SubnibbedView
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let cnatainer = Bundle.main.loadNibNamed("ContainerView", owner: self, options: nil)?.first as! ContainerView
self.view.addSubview(cnatainer)
cnatainer.loadUI()
}
}
class ContainerView: UIView {
#IBOutlet weak var detail: DetailOptionView!
func loadUI(){
print(detail.imageView). // prints nil
}
}
class DetailOptionView: UIView {
#IBOutlet weak var imageView: UIImageView!
}

Pull-to-Refresh is jumpy

I'm looking to implement a Pull-to-refresh, and I'm seeing that the pull-to-refresh action isn't smooth; it makes the Content View jump around the screen, like so:
https://imgur.com/a/6oWPizP
I have the following constraints:
My ViewController looks like this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
#IBOutlet weak var scrollView: UIScrollView!
var refreshControl: UIRefreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(reloadData), for: UIControl.Event.valueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
scrollView.refreshControl = refreshControl
}
#objc func reloadData() {
textLabel.text = "SURPRISE"
refreshControl.endRefreshing()
}
}

How to pass data from a parent view controller to its segmented control views in Swift?

I have a view controller with three segmented view segments. each segment has containers. I want to pass data from the parent view to its segmented views..
class UserDetails: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var view1: UIView!
#IBOutlet weak var view2: UIView!
#IBOutlet weak var view3: UIView!
var id = "120"
i want to pass this variable "id" to view1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view1.hidden = false
view2.hidden = true
view3.hidden = true
print(self.id)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I want to pass variable id to view1..

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

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)
}