I'm trying to add a badge to my UIBarButtonItem and for that I have found a this github :
MIBadgeButton-Swift
but I don't know how to use it.
This is my code which makes my custom UIBarButtonItem:
let shopingCartBTN = UIButton(type: UIButtonType.Custom)
shopingCartBTN.setImage(UIImage(named: "shopingCarBarIcon"), forState: UIControlState.Normal)
shopingCartBTN.imageView?.image = UIImage(named: "shopingCarBarIcon")
shopingCartBTN.frame = CGRectMake(0, 0, 60, 30)//Just increase the width of button
shopingCartBTN.setTitle("5", forState: .Normal)
shopingCartBTN.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside)
let customBarItem = UIBarButtonItem(customView: shopingCartBTN)
self.navigationItem.leftBarButtonItem = customBarItem;
How could I use the MIBadgeButton-Swift to make a badge for my UIBarButtonItem?
Here is two example with custom view and from storyboard
From storyboard by setting custom class :
CODE
#IBOutlet var btnRightBadge: MIBadgeButton!
override func viewDidLoad() {
super.viewDidLoad()
//Custom
let badgeButton : MIBadgeButton = MIBadgeButton(frame: CGRectMake(0, 0, 40, 40))
badgeButton.setTitle("T1", forState: UIControlState.Normal)
badgeButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
badgeButton.badgeString = "1";
let barButton : UIBarButtonItem = UIBarButtonItem(customView: badgeButton)
self.navigationItem.leftBarButtonItem = barButton
//From Storyboard
btnRightBadge.badgeString = "5"
}
OUTPUT
You can also use ENMBadgedBarButtonItem-Swift
Try this once.
//Property
var cartBarbuttonItem:MIBadgeButton?
self.cartBarbuttonItem = MIBadgeButton(frame: CGRectMake(40, 5, 40, 44))
self.cartBarbuttonItem?.initWithFrame(frame: CGRectMake(40, 5, 40, 44), withBadgeString: "0", withBadgeInsets: UIEdgeInsetsMake(15, 2, 0, 15))
self.cartBarbuttonItem?.setImage(UIImage(named: "test"), forState: .Normal)
self.cartBarbuttonItem?.setImage(UIImage(named: "test"), forState: .Selected)
self.cartBarbuttonItem?.addTarget(self, action: Selector("loadCart"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.setLeftBarButtonItem = self.cartBarbuttonItem
Related
I want to set image and title both on rightBarButton. Title will come first. I searched a lot but all the articles are related to leftBarButton. So can anyone please tell me, how to set both of them?
func methodForSettingButtonClicked() {
let buttonSetting = UIButton(type: .custom)
buttonSetting.frame = CGRect(x: 0, y: 5, width: 25, height: 25)
let imageReg = UIImage(named: "setting")
buttonSetting.setTitle("Settings", for: .normal)
buttonSetting.setImage(imageReg, for: .normal)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = buttonSetting
self.navigationItem.rightBarButtonItem = leftBarButton;
}
try barbutton item with custom VIew as follows:-
let button = UIButton(type: .Custom)
button.setImage(UIImage(named: "icon_right"), forState: .Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
button.frame = CGRectMake(0, 0, 53, 31)
button.imageEdgeInsets = UIEdgeInsetsMake(-1, 32, 1, -32)//move image to the right
let label = UILabel(frame: CGRectMake(3, 5, 50, 20))
label.font = UIFont(name: "Arial-BoldMT", size: 16)
label.text = "title"
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.backgroundColor = UIColor.clearColor()
button.addSubview(label)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
imageEdgeInsets plays a important role.
Try it as below and set title as you want
let button = UIButton(type: .system)
button.setImage(UIImage(named: "icon_image_name"), for: .normal)
button.setTitle("Categories", for: .normal)
button.addTarget(self, action: #selector(clickOnButtonActionMethod), for: .touchUpInside)
button.sizeToFit()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
If you want then change button property as you want.
You can do like this way,
let button = UIButton(type: .custom)
button.setTitle("Settings", for: .normal)
button.setImage(#imageLiteral(resourceName: "settings"), for: .normal)
button.semanticContentAttribute = .forceRightToLeft
button.setTitleColor(UIColor.white, for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
It works for me, hope it can help you.
I have a UINavigationController which changes the UIBarButtonItem in the top right corner, depending on the state of the app.
When view did load I initialize the buttons I need
var editBarButton: UIBarButtonItem!
var logoutBarButton: UIBarButtonItem!
var showRepositoryBarButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
editButton = UIButton()
editButton.setImage(UIImage(named: "icon pencil"), forState: .Normal)
editButton.frame = CGRectMake(0, 0, 30, 30)
editButton.addTarget(self, action: #selector(UserProfileNavigatorController.onEditClick), forControlEvents: .TouchUpInside)
editBarButton = UIBarButtonItem(customView: editButton)
let logoutButton = UIButton()
logoutButton.setImage(UIImage(named: "icon logout"), forState: .Normal)
logoutButton.frame = CGRectMake(0, 0, 30, 30)
logoutButton.addTarget(self, action: #selector(UserProfileNavigatorController.onLogoutClick), forControlEvents: .TouchUpInside)
logoutBarButton = UIBarButtonItem(customView: logoutButton)
let showRepositoryButton = UIButton()
logoutButton.setImage(UIImage(named: "icon logout"), forState: .Normal)
logoutButton.frame = CGRectMake(0, 0, 30, 30)
logoutButton.addTarget(self, action: #selector(UserProfileNavigatorController.onShowRepositoryOnWebClick), forControlEvents: .TouchUpInside)
showRepositoryBarButton = UIBarButtonItem(customView: showRepositoryButton)
presenter.viewDidLoad()
}
And depending on the situation, I change the buttons displayed in this way
func showUserProfile() {
navigationItem.setRightBarButtonItems([editBarButton, logoutBarButton], animated: false)
Router.showUserProfileInNavigatorController(self)
}
func showRepository(repository: Repository) {
navigationItem.setRightBarButtonItems([showRepositoryBarButton], animated: false)
Router.showRepository(self, repository: repository)
}
Even though this code is actually executed, nothing appears in the top right corner.
The problem was that that code was running in the UINavigationController. Instead, it's the UIViewController who must show the buttons in the UINavigationController.
So just moving the code inside the UIViewController is working.
Note that there are multiple problems with your code. First, note that while UIButton() will return a non-optional, UIImage may return an optional. So you really have no idea now whether you actually have typed the names of your images correctly. I created a simple project using your code above, with no images in it, and duplicated that if the images are nil, that nothing shows.
Then adding images, I only got two images showing. Looking closely at your code, the 3rd image re-defines loginButton but does not set any properties on showRepositoryButton.
Several suggestions:
us "do { ... }" to create a sub context to avoid unintended variable re-use
if using a UIButton, use the designated initializer (UIButton(type:...)
better still, use a UIBarButtonItem(image: ...)
pay really close attention to optionals, and let forced unwrapping crash at the appropriate time (or use an assert, guard, or precondition)
Without drastically modifying your code, here is what works fine for me:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Howdie!"
do {
let button = UIButton()
let image = UIImage(named: "02-redo")!
button.setImage(image, forState: .Normal)
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(onEditClick), forControlEvents: .TouchUpInside)
editBarButton = UIBarButtonItem(customView: button)
}
do {
let button = UIButton()
let image = UIImage(named: "03-loopback")!
button.setImage(image, forState: .Normal)
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(onLogoutClick), forControlEvents: .TouchUpInside)
logoutBarButton = UIBarButtonItem(customView: button)
}
do {
let button = UIButton()
let image = UIImage(named: "04-squiggle")!
button.setImage(image, forState: .Normal)
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(onShowRepositoryOnWebClick), forControlEvents: .TouchUpInside)
showRepositoryBarButton = UIBarButtonItem(customView: button)
}
navigationItem.setRightBarButtonItems([editBarButton, logoutBarButton, showRepositoryBarButton], animated: false)
}
I was curious about how people make their own search interface, I really do not like the standard and decided to write my own. I do not know how to write my own, so I ask to prompt, where you can see all that I know - that's what is needed to create your own UIView.
An example of what I want to do
I once made something similar, if you use swift ,study this code, it will help you, if this is not helpful explain further please.
func addNavigationBar()
{
self.navigationController!.navigationBar.barTintColor = UIColor.blackColor()
let centerView = UIView(frame: CGRectMake(0, 0, 100, 30))
let logo = UIImage(named: "logo.png")
var btnMiddle = UIButton(frame: CGRectMake(0,5,80,20))
btnMiddle.setBackgroundImage(logo, forState: UIControlState.Normal)
btnMiddle.addTarget(self, action: "goBackHome", forControlEvents: UIControlEvents.TouchUpInside)
centerView.addSubview(btnMiddle)
self.navigationItem.titleView = centerView
var buttonDimension = CGFloat(25)
let rightView = UIView(frame: CGRectMake(0, 0, 100, 30))
let leftView = UIView(frame: CGRectMake(0, 0, 100, 30))
let btnr1 = UIButton(frame: CGRectMake(30,0,buttonDimension,buttonDimension))
btnr1.setBackgroundImage(UIImage(named: "_barSearch.png"), forState: UIControlState.Normal)
btnr1.addTarget(self, action: "search", forControlEvents: UIControlEvents.TouchUpInside)
rightView.addSubview(btnr1)
let btnr2 = UIButton(frame: CGRectMake(80,0,buttonDimension,buttonDimension))
btnr2.setBackgroundImage(UIImage(named: "_barSettinges.png"), forState: UIControlState.Normal)
btnr2.addTarget(self, action: "settinges", forControlEvents: UIControlEvents.TouchUpInside)
rightView.addSubview(btnr2)
let btnl3 = UIButton(frame: CGRectMake(0,0,buttonDimension,buttonDimension))
btnl3.setBackgroundImage(UIImage(named: "_barMenu.png"), forState: UIControlState.Normal)
btnl3.addTarget(self, action: "menu", forControlEvents: UIControlEvents.TouchUpInside)
leftView.addSubview(btnl3)
let rightBtn = UIBarButtonItem(customView: rightView)
self.navigationItem.rightBarButtonItem = rightBtn
let leftBtn = UIBarButtonItem(customView: leftView)
self.navigationItem.leftBarButtonItem = leftBtn
}
I've got this transparent Navigation bar, and I can add a custom icon (code below). However it does not seem to respond to clicks, anyone have any idea why this could be caused?
I'm adding this inside a UIViewController which is inside a UINavigationController.
var button: UIButton = UIButton()
button.setImage(UIImage(named: "customBack"), forState: .Normal)
button.frame = CGRectMake(0, 0, 40, 40)
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
button.targetForAction("doAction:", withSender: button)
button.backgroundColor = UIColor.brownColor()
var leftItem:UIBarButtonItem = UIBarButtonItem()
leftItem.customView = button
self.navigationItem.leftBarButtonItem = leftItem
self.navigationItem.leftBarButtonItem!.action = "doAction:"
self.navigationController!.navigationItem.leftBarButtonItem = leftItem
I know I shouldn't be adding back items myself but in this exact case it is needed. The icon is added on the fly not before loading the view or w/e.
To try it...
var ysBackButton:UIButton = UIButton(frame:CGRectMake(0, 0, kNavBtnSize, kNavBtnSize))
ysBackButton.setImage(UIImage(named: kBack), forState: UIControlState.Normal)
ysBackButton.addTarget(self, action: Selector("backButtonAction"), forControlEvents:UIControlEvents.TouchUpInside)
var leftBarButton:UIBarButtonItem = UIBarButtonItem(customView: ysBackButton)
self.navigationItem.leftBarButtonItem = leftBarButton
Always just after posting I find out:
I was using
button.targetForAction("doAction:", withSender: button)
Whilst I should be doing:
button.addTarget(self, action: "doAction:", forControlEvents: .TouchUpInside)
I am creating a button programatically and I would like to know how to set that button's UIButtonType.
This is how I am creating my button:
let headerPlusButon:UIButton = UIButton(frame: CGRectMake(5, tableView.frame.width - 30, 20, 20))
headerPlusButon.addTarget(self, action: "paymentAddButtonTapped", forControlEvents:.TouchUpInside)
There is no "setButtonTypeFor..." method like how you would change the title's text and obviously this doesn't work:
headerPlusButon.buttonType = UIButtonType.ContactAdd
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender:UIButton!)
{
println("Button tapped")
}
from swift 2 replace UIButton declaration with below line.
let button = UIButton(type: .System) as UIButton
Here is my code
let tutorialButton = UIButton.init(type: .infoDark)
tutorialButton.frame = CGRect(x: self.view.frame.size.width - 20, y: 42, width: 20, height: 20)
tutorialButton.addTarget(self, action: #selector(self.showTutorial), for: .touchUpInside)
view.addSubview(tutorialButton)
I hope it can help you!