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
Related
I want to shift left bar button to extreme left of the navigation bar. by using this code I can able to shift this. I noticed button is not responsive from 20 px from left side(before arrow in yellow rectangle, its not responsive). From middle of the button it is responsive and go to back screen. how to fix this. I used following code
let suggestButton = UIButton(type: UIButton.ButtonType.custom)
suggestButton.frame = CGRect(x: 0, y: 0, width: 60, height: 40)
suggestButton.setImage(backImage, for: UIControl.State.normal)
suggestButton.backgroundColor = .green
suggestButton.imageEdgeInset = UIEdgeInset()top: 10.0, left: 5.0, bottom: 10.0, right : 35)
suggestButton.addTarget(self, action: #selector(self.showPopover(sender:)), for:.touchUpInside)
suggestButton.transform = CGAffineTransform(translationX: -15, y: 0)
let suggestButtonContainer = UIView(frame: suggestButton.frame)
suggestButtonContainer.addSubview(suggestButton)
let suggestButtonItem = UIBarButtonItem(customView: suggestButtonContainer)
navigationItem.leftBarButtonItem = suggestButtonItem
For some reason my iPhone/Simulator incorrectly interprets the black when I assign it to the tabBar.
UITabBar:
func setupViewController(){
tabBar.barTintColor = .black
}
The custom button:
func setupMiddleButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 74, height: 74))
...
menuButton.backgroundColor = .black
tabBar.addSubview(menuButton)
}
I'd like that the tabBar color is true black (#000000) as the round button. I also tested it with other colors, the problem (difference) is the same. Any hint would be very kind!
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)
I'm trying to dynamically set my navigation bar's text so that the header text always fits. I'm acccomplishing that like this:
// Pet's Day text "Joy's Day"
if let range = currentPet.range(of: "_") {
let petsName = currentPet[range.upperBound..<currentPet.endIndex]
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "\(petsName)'s Day"
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navBar.topItem?.titleView = titleLabel
}
However, as seen by this picture, this lowers the header text below its natural height:
The navigation bar on the left is from one of my app's other views, while the one on the right is the one I'm setting.
Both of these navigation bars are navigation bars that I've dragged in and made the prompt equal to an empty string to increase their height:
Can anybody please help me implement my code above so that it doesn't drop down the header text?
**Edit: Here are screenshots from Xcode's debug hierarchy:
This is the normal navigation bar:
This is the one I'm setting:
You need to set the baseline, by default is Align Baseline, you need to change to Align Centers
Code example:
class TestViewController: UIViewController {
#IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// Change the height of the navbar
self.navBar.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 68)
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "Log Events" // Change to Joy's Day and check the result
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.baselineAdjustment = .alignBaselines
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navBar.topItem?.titleView = titleLabel
}
}
I would like to add a cycle view and a label to UINavigation. like this:
I can set a label to my UINavigation by this code:
if let navigationBar = self.navigationController?.navigationBar {
let firstFrame = CGRect(x: 300, y: 0, width: navigationBar.frame.width/2, height: navigationBar.frame.height)
let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "First"
navigationBar.addSubview(firstLabel)
}
but I have two problems by this code:
1.how to set x position correctly?
(to test I set 300 value, but this value show different position on different screen sizes)
2. how to set a cycle background to this label ?
You can add both of the view (red circle) and the label (number 16) programmatically as a subView to the button of the bar button item.
What you should do is:
Connect the button as an IBOutlet to its ViewController:
Make sure that the connected component is the UIButton, but NOT UIBarButtonItem.
As you can see, I called it btnMenu.
Create your views (red circle and number label) and add it to the btnMenu:
It should be similar to:
import UIKit
class ViewController: UIViewController {
//...
#IBOutlet weak var btnMenu: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//...
// 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: 4, 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)
//...
}
//...
}
And that's it!
UIButton is a subclass of UIView, meaning that you can add subviews to it (addSubview(_:)).
Output:
Hope this helped.