Refresh View After Data Changes - ios

Im trying to make real time chat, It is going good but I have a problem.
I have added a subview to navigationController instead of title. I have an avatar, full name of user and status in this view like whatsapp. I have got user is online string from pusher service and I want to display it in my subview bottom of user full name like whatsapp. How can I refresh the view when I get online string from func onPresenceChanged(stateChange method ?
I have added self.navbar.setNeedsDisplay() but it is not working.
ChatViewController.swift (I cropped it for quick review)
class ChatViewController: UIViewController, UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PCRoomDelegate {
var navbarView = UIView()
var navbarAvatar = UIImageView()
var navbar = UILabel()
var statusString = [String]()
var usernameString = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.navbarView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4, height: 44.0))
self.navbarView.backgroundColor = UIColor.clear
self.navbar = UILabel(frame: CGRect(x: 44.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4 - 44, height: 44.0))
self.navbar.backgroundColor = UIColor.clear
self.navbar.numberOfLines = 2
self.navbar.textAlignment = NSTextAlignment.left
let bodyText = NSMutableAttributedString(string: "Halil İbrahim YÜCE", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
usernameString.append("\n#" + "halilyc")
bodyText.append(NSAttributedString(string: usernameString.last!, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13), NSAttributedString.Key.foregroundColor : UIColor.gray]))
self.navbar.attributedText = bodyText
self.navbarView.addSubview(navbar)
self.navigationItem.titleView = self.navbarView
}
func onPresenceChanged(
stateChange: PCPresenceStateChange,
user: PCUser
) {
print("User \(user.displayName)'s presence changed to \(stateChange.current.rawValue)")
self.statusString.append(stateChange.current.rawValue)
self.navbar.setNeedsDisplay()
if statusString.count != 0 {
self.usernameString.append("\n#" + statusString.last!)
}
}
}

Assigning a string variable to a UILabel's attributedText property doesn't create a binding between that string and the label; you need to update attributedText if you want to see an updated value in your UI. Also, UI updates must be performed on the main thread; your update from the network will be delivered on a background thread.
Finally, I don't see any need to keep an array of statuses, you just need the most recent.
You can refactor to create a computed variable that provides the current user status text for your navigation bar.
It isn't clear how you are intending to display the display name, the username and the status (There are 3 things, but only 2 lines). I have just made it three lines and you can adapt as you require.
class ChatViewController: UIViewController, UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PCRoomDelegate {
var navbarView: UIView!
var navbarAvatar: UIImageView!
var navbar: UILabel!
var status: String? {
didSet {
self.updateNavText()
}
var username: String? {
didSet {
self.updateNavText()
}
var displayName: String?
didSet {
self.updateNavText()
}
var statusText: NSAttributedString {
let text = NSMutableAttributedString(string: displayName ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
if let username = self.username {
text.append("\n#\(username)")
}
if let status = self.status {
text.append("\n\(status)")
}
return text
}
override func viewDidLoad() {
super.viewDidLoad()
self.navbarView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4, height: 44.0))
self.navbarView.backgroundColor = UIColor.clear
self.navbar = UILabel(frame: CGRect(x: 44.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4 - 44, height: 44.0))
self.navbar.backgroundColor = UIColor.clear
self.navbar.numberOfLines = 2
self.navbar.textAlignment = NSTextAlignment.left
let bodyText = NSMutableAttributedString(string: "Halil İbrahim YÜCE", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
self.updateNavText()
self.navbarView.addSubview(navbar)
self.navigationItem.titleView = self.navbarView
}
func onPresenceChanged(
stateChange: PCPresenceStateChange,
user: PCUser
) {
print("User \(user.displayName)'s presence changed to \(stateChange.current.rawValue)")
DispatchQueue.main.async {
self.status = stateChange.current.rawValue
}
}
func updateNavText() {
self.navbar.attributedText = self.statusText
}

You can manage things like:
First, create a function to set up your all UI objects adding on viewController's view or whatever you want to add. Call this method (like setupInitialLayout()) in viewDidLoad(). By calling this method in viewDidLoad(), because you just need to add components only once and you don't need to call this method again.
Now create another method in which write your all code like set your data whatever you want to display. Call this method (like setupData()) in viewWillAppear(). Also, you can call this method setupData() whenever you need like just user is online you are chatting with and you want to show as Online, just call this method only.

Related

Firebaseui on iOS: can't set background color on customized subclassed login screen

I'm subclassing the login screen of Firebaseui with:
import UIKit
import FirebaseUI
class LoginViewControllerCustom: FUIAuthPickerViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
let arr = Bundle.main.loadNibNamed("LoginText", owner: nil)!
let v = arr[0] as! UIView
self.view.addSubview(v)
}
}
My implementation works as I see the xib LoginText loaded on login screen.
But the background color is royally ignored.
How to enforce a bg color on the login screen from that subclass?
Edit: if I apply the answer below with view.insertSubview(imageViewBackground, at: 0)
Here is what I get:
As you can see the image gets inserted under the view that holds the login button. If I set "at: 1" it completely cover the buttons and they can't be used.
I resolved the problem in an unexpected way.
On the delegate method that would load this controller, I changed:
func authPickerViewController(forAuthUI authUI: FUIAuth) -> FUIAuthPickerViewController {
return LoginViewControllerCustom(authUI: authUI)
}
to
func authPickerViewController(forAuthUI authUI: FUIAuth) -> FUIAuthPickerViewController {
return LoginViewControllerCustom(nibName: nil, bundle: Bundle.main, authUI: authUI)
}
The addition of Bundle.main solved the issue, and replaced the original controller by mine, which was several levels deeper until that.
Not sure exactly why, but this did solve the issue.
you can try to put "fake" image background:
override func viewDidLoad() {
super.viewDidLoad()
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width:
width, height: height))
imageViewBackground.backgroundColor = .red
view.insertSubview(imageViewBackground, at: 0)
let arr = Bundle.main.loadNibNamed("LoginText", owner: nil)!
let v = arr[0] as! UIView
self.view.addSubview(v)
}
Edit: try this it's not elegant but it solves the problem.
override func viewDidLoad() {
let scrollView = view.subviews[0]
scrollView.backgroundColor = .clear
let contentView = scrollView.subviews[0]
contentView.backgroundColor = .red
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let backgroundImage = UIImageView(frame: CGRect(x: 0, y: -1, width: width, height: height))
view.backgroundColor = .red
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
view.insertSubview(backgroundImage, at: 0)
}

Swift Custom UIView with init parameters

I have witten a custom UIView class which takes several parameters and overrides an empty storyboard UIView(subclassed as MultiPositionTarget class instead of UIview) as below.The views below are linked as outlet to view controller as it is seen from the code below(in total 9 views).
// Initialize the targets
target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")
target1.parentVC = self
targetList.append(target1)
However, it does not accept the parameters. And only loads the view. Here is my class below:
class MultiPositionTarget: UIView{
var targetName: String!
var bottomLabelName: String!
var imageName: String!
var isSelected: Bool!
var labelTop: UILabel!
var labelBottom: UILabel!
var parentVC: SelectTargetViewController!
init(_ frame: CGRect,_ imagName: String,_ targetname: String,_ targetlabel: String){
self.imageName = imagName
self.targetName = targetname
self.bottomLabelName = targetlabel
super.init(frame: frame)
setUp()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}
func setUp(){
let viewWidth = self.bounds.width
let viewHeight = self.bounds.height
// Add top label
labelTop = UILabel(frame: CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight/5))
//labelTop.center = CGPoint(x: 160, y: 285)
labelTop.textAlignment = .center
labelTop.font = UIFont(name:"System",size:6)
labelTop.text = self.imageName
labelTop.textColor = UIColor.white
labelTop.backgroundColor = hexStringToUIColor(hex: "#770B2C")
//labelTop.alpha = 0.5
self.addSubview(labelTop)
let image = UIImage(named: "targetTemp.png")
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: (viewHeight-viewHeight * 4/5), width: viewWidth, height: (viewHeight * 4/5))
self.addSubview(imageView)
// Add bottom Label
labelBottom = UILabel(frame: CGRect(x: 0, y: (viewHeight * 4/5), width: viewWidth, height: viewHeight/5))
//labelBottom.center = CGPoint(x: 160, y: 285)
labelBottom.textAlignment = .center
labelBottom.text = self.bottomLabelName
labelBottom.font = UIFont(name:"System",size:10)
labelBottom.textColor = UIColor.white
labelBottom.backgroundColor = hexStringToUIColor(hex: "770B2C")
labelBottom.alpha = 0.95
self.addSubview(labelBottom)
self.isUserInteractionEnabled = true
let viewTap = UITapGestureRecognizer(target: self, action: #selector(singleTap))
self.addGestureRecognizer(viewTap)
}
}
Any help or hint is appreciated. Thanks in advance stack overflow family.
This line is creating a new view:
target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")
The new view is unrelated to the view shown on the screen. The new view isn't actually on the screen at all, because you haven't added it to the view hierarchy. Even if you did, it's invisible because it has a frame of zero.
In short, you can't call your custom initialiser and also design your view in the storyboard. You can initialise the properties you want either in the storyboard as well, or in code.
If you want to initialise the properties in the storyboard as well, you can modify imageName, bottomLabelName and targetName with #IBInsepctable:
#IBInsepctable var targetName: String!
#IBInsepctable var bottomLabelName: String!
#IBInsepctable var imageName: String!
// or, if you want to directly select an image from the storyboard:
// #IBInsepctable var image: UIImage!
This way those properties will show up in the property inspector in the storyboard.
If you want to initialise it in code, you must set it with assignment statements, and not with an initialiser:
target1.targetName = "David"
target1.bottomLabelName = "Target"
target1.imageName = "targetTemp.png"
target1.parentVC = self
targetList.append(target1)

iOS swift: add kern space in the tab bar text

I'm not able to add kern space into the tab bar attributed text.
The UITabBar in question is a custom tabBar, you can find the code below.
I'm using the "attributed key" dictionary to add attributes to the items title, but I'm having an issue with the kern space.
class ProfileTabBar: UITabBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setStyle()
}
required override init(frame: CGRect) {
super.init(frame: frame)
self.setStyle()
}
func setStyle() {
self.tintColor = Style.shared.primary1
// Disable the default border
self.layer.borderWidth = 0.0
self.clipsToBounds = true
// Create a new bottom border
let bottomLine = CALayer()
let screenWidth = UIScreen.main.bounds.width
//let viewForFrame = self.superview ?? self
//let screenWidth = viewForFrame.bounds.width
bottomLine.frame = CGRect(x: 0.0, y: self.frame.height - 1, width: screenWidth, height: 2.0)
bottomLine.backgroundColor = UIColor(red: 235.0/255, green: 235.0/255, blue: 235.0/255, alpha: 1.0).cgColor
self.layer.addSublayer(bottomLine)
// Get the size of a single item
let markerSize = CGSize(width: screenWidth/CGFloat(self.items!.count), height: self.frame.height)
// Create the selection indicator
self.selectionIndicatorImage = UIImage().createSelectionIndicator(color: self.tintColor, size: markerSize , lineWidth: 3.0)
// Customizing the items
if let items = self.items {
for item in items {
item.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -15)
let attributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.font: UIFont(name: Style.shared.fontBold.fontName, size: 14) as Any,
NSAttributedStringKey.kern: NSNumber(value: 1.0)
]
item.setTitleTextAttributes(attributes, for: .normal)
}
}
}
All the attributes works except for the kern. What I'm doing wrong?
This question is old and there is an even older answer here. It appears that UITabBarItem appearance ignores NSAttributedString.Key.kern. That leaves us with a few options.
Subclass UITabBarItem this isn't easy because UITabBarItem inherits from UIBarItem which is an NSObject not a UIView.
Subclass UITabBar this can be done, but involves a decent amount of work for just some kern. You'll have to use UIButton instead of UITabBarItem so that the kern is applied.
You can add spacing using unicode characters in your title. This is really easy and can probably achieve the spacing you're looking for with just a few lines of code.
Unicode spacing:
U+0020 1/4 em
U+2004 1/3 em
U+2005 1/4 em
U+2006 1/6 em
U+2008 The width of a period “.”
U+2009 1/5 em (or sometimes 1/6 em)
You can use a unicode character in a String in Swift like this "\u{2006}". That means we can insert a small space between all the characters in our tabBarItem title. Like this:
extension String {
var withOneSixthEmSpacing: String {
let letters = Array(self)
return letters.map { String($0) + "\u{2006}" }.joined()
}
Using this for our tabBarItems:
self.navigationController.tabBarItem = UITabBarItem(
title: "Home".withOneSixthEmSpacing,
image: homeImage,
selectedImage: homeSelectedImage
)
Visually we end up with:
Instead of:
Another workaround is to subclass UITabBarController, and set the kerning in viewDidLayoutSubviews.
class FooTabBarController: UITabBarController {
private var tabBarButtons: [UIControl] {
tabBar.subviews.compactMap { $0 as? UIControl }
}
private var tabBarButtonLabels: [UILabel] {
tabBarButtons.compactMap { $0.subviews.first { $0 is UILabel } as? UILabel }
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.tabBarButtonLabels.forEach {
if let attributedText = $0.attributedText {
let mutable = NSMutableAttributedString(attributedString: attributedText)
mutable.addAttribute(.kern, value: 0.5, range: .init(location: 0, length: mutable.length))
$0.attributedText = mutable
$0.sizeToFit()
}
}
}
}
The caveats to this solution are:
It is somewhat fragile. It can break if Apple changes the view structure in the tab bar, ie if they stop using UIControl, or if they change the subview heirarchy.
It isn't all that efficient because the kerning has to be set every layout cycle.
I loved #DoesData's answer, it really helped me out a lot.
Here's a more "swifty" version of it I came up with if it helps anyone:
extension String {
var withAddedSpacing: String {
Array(self)
.compactMap { String($0) }
.joined(separator: "\u{2006}")
}
}

Calling a function (containing variable) in another class

I have a relatively simple question. I created a function - covers the contents to retrieve data from the API. I need to call a function in several different classes (don't want to duplicate). I am calling that function, but nothing is getting displayed. If this function is a part of class where I want to call it, it works correctly. Where is the problem, please let me know. (I think in UIView...)? Thank you.
My function:
func loadScreen() {
var loadView:UIView!
loadView = UIView(frame: self.view.frame)
loadView!.backgroundColor = UIColor.black
loadView!.alpha = 1.0
let actView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
actView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
actView.center = CGPoint(x: self.view.bounds.size.width / 2 , y: self.view.bounds.size.height / 2 )
actView.startAnimating()
loadView?.addSubview(actView)
self.view.addSubview(loadView!)
}
I call a function in another class in viewDidLoad (), but as I said, nothing is displayed.
First create a singleton class like this
Singleton class
import UIKit
import Foundation
class Singleton: NSObject
{
static let sharedInstance = Singleton()
override init()
{
super.init()
}
func placeBlockerView(target : UIViewController)
{
var loadView:UIView!
loadView = UIView(frame: target.view.frame)
loadView!.backgroundColor = UIColor.black
loadView!.alpha = 1.0
let actView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
actView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
actView.center = CGPoint(x: target.view.bounds.size.width / 2 , y: target.view.bounds.size.height / 2 )
actView.startAnimating()
loadView?.addSubview(actView)
target.view.addSubview(loadView!)
}
}
After that use it from where ever you want as required
like this
Singleton.sharedInstance.placeBlockerView(target: self)
This above line will add that UIView you have created to your view controller's view.
Let me know if it don't work.
Instead of adding it in the above, it is always advised to return the UIView from that method and in your subclass of UIViewController do the following.
func viewDidLoad() {
let loadView = loadScreen()
self.view.addSubView()
}

Programmatically set the UITabBarItem icon for every linked ViewController

Currently we are changing the image shown in the UITabBarItem like this:
override func viewDidLoad() {
super.viewDidLoad()
// register additional NIB files
tableView.registerNib(UINib(nibName: "WeekDayTableViewCell", bundle: nil), forCellReuseIdentifier: "WeekDayCell")
tabBarItem.title = "Week".localized
tabBarItem.image = UIImage.fontAwesomeIconWithName(FontAwesome.Calendar, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
}
The problem with this is, that you have to click on every tab to load the corresponding images.
I thought I could change the images in the UITabBarViewController if I get the list of all UITabBarItems. But this list is always empty.
Which is the correct way to do this?
An alternative—perhaps a more Swift-y—way to do this is to set the icons from a Dictionary of tabs. We have an enum TabTitles for the names of the tabs, and tabIcons to look it up.
This way, there's not so much to remember to change if you add a tab or change the order.
private enum TabTitles: String, CustomStringConvertible {
case Stacks
case Settings
case Clusters
case Services
case Registries
private var description: String {
return self.rawValue
}
}
private var tabIcons = [
TabTitles.Stacks: FontAwesome.Clone,
TabTitles.Settings: FontAwesome.Gears,
TabTitles.Clusters: FontAwesome.Cubes,
TabTitles.Services: FontAwesome.Exchange,
TabTitles.Registries: FontAwesome.Institution
]
override func viewDidLoad() {
super.viewDidLoad()
if let tabBarItems = tabBar.items {
for item in tabBarItems {
if let title = item.title,
tab = TabTitles(rawValue: title),
glyph = tabIcons[tab] {
item.image = UIImage.fontAwesomeIconWithName(glyph, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
}
}
}
}
I found out, if I add this code to the viewDidAppear in the UITabBarController it will work.
let tabBarItems = tabBar.items! as [UITabBarItem]
tabBarItems[0].title = "Week".localized
tabBarItems[0].image = UIImage.fontAwesomeIconWithName(FontAwesome.Calendar, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
tabBarItems[1].title = "Settings".localized
tabBarItems[1].image = UIImage.fontAwesomeIconWithName(FontAwesome.Gears, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
Swift 5
You can set tabBarItems icons from your first controller 'viewDidLoad' method.
func setupTabBar() {
tabBarController?.tabBar.items![0].title = "Week"
tabBarController?.tabBar.items![0].image = #imageLiteral(resourceName: "icons8-clinic")
tabBarController?.tabBar.items![1].title = "Profile"
tabBarController?.tabBar.items![1].image = #imageLiteral(resourceName: "icons8-phone_not_being_used_filled")
}
Use item.image = UIImage.fontAwesomeIcon(name: glyph, textColor: UIColor.black, size: CGSize(width: 30, height: 30)) for Swift 3.
Also remember to import FontAwesome_swift

Resources