adding custom navigation bar in viewwillappear makes navigation slow? - ios

I have created a custom navigation bar.Now i am adding this into viewwillappear method.Due to this when i return to my view controller it takes time to load.
func addViewToNav()
{
myView = UIView(frame: CGRect(x: 0, y: 20, width: (self.navigationController?.navigationBar.frame.size.width)!-40, height: (self.navigationController?.navigationBar.frame.size.height)!))
imageView.frame = CGRect(x: 5, y: (myView?.frame.size.height)!/2-14, width: 28, height: 28)
let image = UIImage(named: "download")
imageView.contentMode=UIViewContentMode.scaleAspectFill
imageView.image=image
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true
imageView.backgroundColor=UIColor.yellow
label = UILabel(frame: CGRect(x: imageView.frame.size.width+15, y: 0, width: 150, height: (myView?.frame.size.height)!))
label?.text="Label"
label?.textColor=UIColor.white
myView?.addSubview(imageView)
myView?.addSubview(label!)
self.navigationController?.view.addSubview(myView!)
//add tap gesturte on imageview & label
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
myView?.isUserInteractionEnabled=true
tapGesture.numberOfTapsRequired=1
myView?.addGestureRecognizer(tapGesture)
}
Please guide how can i make it fast because this is very slow.

It is taking time because every time the controller appears on the screen viewWillAppear will be called and each time the code for creating the custom view, adding it as the subview and then adding gesture recognizer will be executed.
There are 2 ways that might reduce the processing time:
If you want to do it programatically, the add the code to create and add custom navigation bar in viewDidLoad instead of viewWillAppear
Add the custom navigation bar in the storyboard itself and create an outlet connection for it in the controller.

Related

Custom View in Navigationbar titleView disappear iOS Swift

I'm having a problem with the titleView in navigation bar in iOS. I've created a custom view for the titleView, but every time I push another controller, the titleView immediately appear and disappear when I go back to the first view controller. Here's my code.
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "img_appbar_logo")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: 40))
logoContainer.backgroundColor = UIColor.clear
animatedLogo = UIImageView(frame: CGRect(x: 0, y: 0, width: logoContainer.frame.width, height: logoContainer.frame.height))
animatedLogo.contentMode = .scaleAspectFit
animatedLogo.clipsToBounds = true
animatedLogo.image = logo
logoContainer.addSubview(animatedLogo)
navigationItem.titleView = logoContainer
}
I've already fixed the issue related to titleView in Navigation bar. I found out that after I push another controller and pop back, the titleView will be replaced by the view of empty UILabel in the NavigationItem. That's why the titleView appear then disappear.
How I fixed the issue?
I added the custom view directly to the navigation bar. Here's the code
let logo = UIImage(named: "img_appbar_logo")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
logoContainer = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: 40))
logoContainer.backgroundColor = UIColor.red
animatedLogo = UIImageView(frame: CGRect(x: 0, y: logoContainer.frame.origin.y, width: logoContainer.frame.width, height: logoContainer.frame.height))
animatedLogo.contentMode = .scaleAspectFit
animatedLogo.clipsToBounds = true
animatedLogo.image = logo
logoContainer.addSubview(animatedLogo)
navigationController?.navigationBar.addSubview(logoContainer) <--- new
//navigationItem.titleView = logoContainer <---- old
Then remove the view on viewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
logoContainer.removeFromSuperview()
}
Set the titleView in viewWillAppear since the titleView gets replaced by the title in the previous controller

swift 4 UITableView Footer, absolute bottom

I a have created a footer for my UITableView like so:
let customView = UIView(frame: CGRect(x: 0, y: self.view.bounds.height - 50, width: self.view.bounds.width, height: 50))
customView.backgroundColor = UIColor.red
commentText = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
commentText.backgroundColor = UIColor.white
commentText.layer.borderColor = UIColor.gray.cgColor
commentText.layer.borderWidth = 1
commentText.borderStyle = .roundedRect
commentText.placeholder = "Enter Comment"
let submitButton = UIButton(frame: CGRect(x: self.tableView.bounds.width - 50, y: 0, width: 50, height: 50))
submitButton.setTitle("Go", for: .normal)
submitButton.backgroundColor = UIColor.blue
submitButton.addTarget(self, action: #selector(self.submitButtonPressed(_:)), for: .touchUpInside)
customView.addSubview(commentText)
customView.addSubview(submitButton)
tableView.tableFooterView = customView
and here is the result of it in the screenshot:
What I am trying to do is have the footer always at the bottom of the screen, is this possible?
A table view footer is always at the end of the table view, after the last row in the table view. This is true no matter how many rows are in the table view.
If you want a view that stays at the bottom of the screen and never scrolls no matter how many rows there are, then you don't want a table view footer. You simply want a view added to the bottom of the screen.
The best solution is to use a UIViewController that has your "footer view" pinned to the bottom and a table view added that fills the rest of the screen.
maybe you can just add the footer view at the bottom of the screen view, not add it as a footer view of UITableView, since you don't want the textfield and submit button to move when UITableView scroll.
You can just add your custom view to the view itself instead of the tableview.
view.addSubView(customView)

How to set a button to most right side of navigation - iOS

I set a button to right side of navigation bar:
my view controller:
I want to set a burger icon,red cycle and a label to this button. like this:
my code:
self.navigationController?.navigationBar.barTintColor = self.utilities.hexStringToUIColor(hex: "#00b8de")
var imageview2 = UIImage(named: "menulogo")
imageview2 = imageview2?.imageResize(sizeChange: CGSize(width: 25, height: 25))
btnMenu.setImage(imageview2,for:UIControlState.normal)
btnMenu.setTitle("", for: .normal)
// setup the red circle UIView
let redCircleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
redCircleView.backgroundColor = UIColor.red
redCircleView.layer.cornerRadius = view.frame.size.width / 2
// setup the number UILabel
let label = UILabel(frame: CGRect(x: 30, y: 0, width: 20, height: 20))
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 10)
label.text = "16"
// adding the label into the red circle
redCircleView.addSubview(label)
// adding the red circle into the menu button
btnMenu.addSubview(redCircleView)
with above codes I have three problems:
my burger image isn't in most right side of navigation.
my cycle view doesn't show
my icon is white, but it shows blue!
btnMenu is my button in navigation.
I have use this library for set badge on navigation button.
MIBadgeButton is badge button written in Swift with high
UITableView/UICollectionView performance.
https://github.com/mustafaibrahim989/MIBadgeButton-Swift

Create UI chat like whatsapp and how to create event touch in navigation bar (iOS xamarin)

I would like to ask about iOS xamarin. I'm creating an application similar to whatsapp, but I ran into the following problems:
Creating an UI like whatsapp in navigation bar (title bar), I dragged ImageView/Label on there, but the ImageView/Label was covered by navigation bar. Just like in the picture below:
When the navigation bar is clicked, I want to direct the user to another controller, but I don't know how to create the event touch/click in navigation bar. Could you give me some pointers on that?
That's do not touch on Navigation bar, its touch on Button placed on the navigation bar. long press on Title /name in WhatsApp, and you will see highlighted area-- (touch portion) - which is a button.
let buttonContainer = UIView(frame: CoreGraphics.CGRect(x: 0, y: 0, width: 200, height: 44));
buttonContainer.backgroundColor = .clear
let lbltitle = UILabel(frame: CoreGraphics.CGRect(x: 0, y: 0, width: 200, height: 44))
lbltitle.text = "TITLE TO DISPLAY"
buttonContainer.addSubview(lbltitle)
let btnTitle = UIButton(type: .custom)
btnTitle.frame = CoreGraphics.CGRect(x: 0, y: 0, width: 200, height: 44);
btnTitle.backgroundColor = .clear;
buttonContainer.addSubview(btnTitle)
btnTitle.removeTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
navigationItem.titleView = buttonContainer
And here is code to push the view controller on the button tap.
#objc func buttonTapped()
{
navigationController?.pushViewController(ChatVC, animated: false)
}

Putting imageview on navigation bar

I want to add image to right side of navigation bar. But I can't drag image view to navigation bar. I can only drag a button.
How can I do this ? I want same thing with whatsapp profile image. You know they have circle profile picture on right side of navigation bar.
Drag a UIView and drop it on the navigation bar first. Then drag a UIImageView into the view.
That should give you what you are looking for.
Set the image view height and width using constraints. You have to set the width and height of the containing view in the size inspector.
**This will Display the image on right side on navigation bar**
func viewDidLoad(){
let containView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
let imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageview.image = UIImage(named: "photo.jpg")
imageview.contentMode = UIViewContentMode.scaleAspectFit
imageview.layer.cornerRadius = 20
imageview.layer.masksToBounds = true
containView.addSubview(imageview)
let rightBarButton = UIBarButtonItem(customView: containView)
self.navigationItem.rightBarButtonItem = rightBarButton
}
Don't know about storyboards. But from code you can create UIBarButtonItem with custom view.
let customView = UIImageView()
let customViewItem = UIBarButtonItem(customView: customView)
self.navigationItem.rightBarButtonItem = customViewItem
From storyboard:
Just drag UIBarButtonItem into your controller navigation bar. In element menu(Attribute inspector) select identifier: Custom and Image: the image you want.

Resources