How to add Header/Custom View to JSQMessagesViewController? - ios

I'm using a storyboard to build my UIs. I wasn't able to find same ticket with same question on JSQMessageViewController's repo on Github.
Basically, I want to add a custom view, say a header in my controller, like so:
What I did so far:
Add the view to the storyboard - didn't work.
I programmatically made a UICollectionReusableView and add it to the data source function: viewForSupplementaryElementOfKind
Any ideas?

You can change anything you like but here is how to do it very simply programatically.
call addViewOnTop() this in your viewDidLoad
func addViewOnTop() {
let selectableView = UIView(frame: CGRect(x: 0, y: 60, width: self.view.bounds.width, height: 40))
selectableView.backgroundColor = .red
let randomViewLabel = UILabel(frame: CGRect(x: 20, y: 10, width: 100, height: 16))
randomViewLabel.text = "RandomView"
selectableView.addSubview(randomViewLabel)
view.addSubview(selectableView)
}
Add an UIEdgeInset to let you view your messages under the new view
Call this in the viewDidLoad()
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)

Related

How to add label to a subview?

I have a skscene in which i show subview and i want a "Game Over" label in it.
var gameoversub = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
func addsubview() {
gameoversub.backgroundColor = UIColor.black
self.view?.addSubview(gameoversub)
gameoversub.center = (super.scene?.anchorPoint)!
gameoversub.bounds = CGRect(x: 0, y: 0, width: (super.scene?.frame.size.width)!, height:(super.scene?.frame.size.height)!)
gameoversub.addSubview(titleLabel)
}
It says, I can't add SKLabelNode, it wants UIView. Some solutions?

Centering a button in UIScrollView

For the life of me, I can't figure out how to center a UIButton in UIScrollView...
let sv = UIScrollView(frame: CGRect(x: 0, y: 0, width: 300, height: 600))
let b = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
sv.addSubview(b)
b.center = sv.center
No matter what I do, the button seems to be off center. This logic works in normal UIView. Why doesn't it with UIScrollView?
you can also use
let b = UIButton(frame: CGRect(x: sv.frame.size.width/2 -15, y: sv.frame.size.height/2 -15, width: 30, height: 30))
for your code first check value of sv.center, if it is OK and your UI is not updating then call layoutIfneeded
Reference : How is layoutIfNeeded used?

Height of UINavigationBar in UITableView doesn't change

I have a tabbed application, containing as a first tab "Home", which is the initial view controller. Home is a UITableViewController and conforms to BETableViewController, as all the tabs of the app, and is embeded in a UINavigationViewController.
When you select a row in Home, you get to ArticleViewController, conforming to BEViewController .
BEViewController and BETableViewController have a large (height has been enlarged) UINavigationBar containing a logo; those changes come in the viewDidLoad() function of each Controller.
The problem is that, when i load the app, Home shows a normal sized UINavigationBar with the logo protrudind into the TableView.
. When I select a row, it takes me to ArticleViewController, which does show the wanted results in the navigation bar. Wheni go back to Home, through the back button, Home shows the intended results.
I'll include the screenshots and some code.
class BEViewController: UIViewController {
var logo = UIImageView() //CGSize(width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
logo.frame = CGRect(x: self.view.frame.width / 2 - 40, y: 20, width: 80, height: 80)
logo.image = #imageLiteral(resourceName: "logo")
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100)
self.navigationController?.navigationBar.addSubview(logo)
}
}
class BETableViewController: UITableViewController {
var logo = UIImageView() //CGSize(width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
logo.frame = CGRect(x: self.view.frame.width / 2 - 40, y: 20, width: 80, height: 80)
logo.image = #imageLiteral(resourceName: "logo")
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100)
self.navigationController?.navigationBar.addSubview(logo)
}
}
Image 1: Initial Home
Image 2: ArticleViewController
Please set the size of image in Navigation Bar less than 49 pt. Please refer Apple Guidelines - Navigation Bar

How do I add two UILabel to a single TableView?

I'm making a message when the TableView is empty, this is code :
let emptyLabel=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
let emptyLabel2=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
emptyLabel.text = "no reminder added yet"
emptyLabel.textColor=UIColor.darkGray
emptyLabel.font=UIFont(name: "HelveticaNeue-Light", size: 18)
emptyLabel.textAlignment = NSTextAlignment.center
emptyLabel2.text = "you add by going back to the homescreen"
emptyLabel2.textColor=UIColor.darkGray
emptyLabel2.font=UIFont(name: "HelveticaNeue-Light", size: 12)
emptyLabel2.textAlignment = NSTextAlignment.center
self.tableView.backgroundView = emptyLabel2
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
return 0
It works, but how do I add emptyLabel2 beneath the emptyLabel. I want to use the defined label properties.
If you're trying to add your 2 labels to the superview, with emptyLabel2 on top of emptyLabel, all you need to do is this:
self.view.addSubview(emptyLabel)
self.view.addSubview(emptyLabel2)
They will be added in the order you call addSubview.
If you are asking about how to define the frame so that they show one before the next, you need to modify how you are initializing. Something like this (for example):
let emptyLabel=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: 20))
let emptyLabel2=UILabel(frame: CGRect(x: 0,y: 20, width: self.view.bounds.width, height: 20))
self.view.addSubview(emptyLabel)
self.view.addSubview(emptyLabel2)
In reality you may want to find a better way to set the size, or just make 1 label with attributedText so that you do not need 2 labels.

How to show a view in every screen at bottom

I would like to add a bottom view in all view controllers which will show the progress of Audio or video play.I need Like this
i did in the following
let window = UIApplication.sharedApplication().keyWindow!
let v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
window.addSubview(v);
v.backgroundColor = UIColor.blackColor()
let v2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
v2.backgroundColor = UIColor.whiteColor()
window.addSubview(v2)
But its not displaying in every screen.
I have an approach for you.
Firstly you have to manage this in global way.
You need to create an Object of AppDelegate. And make a function in that class in which you can code
Then on loading your main view, you need to call that function so it will be visible as you want.

Resources