I got the problem when i use Custom Bar button with image and title, I use below code
let button = UIButton(type: .system)
button.titleEdgeInsets.left = 5
button.setImage(buttonImg, for: .normal)
button.setTitle(title, for: .normal)
button.sizeToFit()
button.addTarget(self, action: #selector(self.popVC), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
And Here is the problem snippet. Text not show clearly
Try this:
let backButton = UIButton(type: .system)
backButton.frame = CGRect(x: 0, y: 0, width: 500, height: 30)
backButton.setImage(buttonImg, for: .normal)
backButton.setTitle(title, for: .normal)
backButton.addTarget(self, action: #selector(self.popVC(_:)), for: .touchUpInside)
backButton.sizeToFit()
let backBarButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backBarButton
Related
let historyButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(named: "AccentColor")
button.setTitle("History", for: .normal)
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 30 / 2
button.titleLabel?.font = Constants.Fonts.bold(size: 12)
button.addTarget(.none, action: #selector(historyPressed), for: .allTouchEvents)
return button
}()
fileprivate func setupNavBar() {
navigationItem.title = "Introductions"
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: historyButton)
}
the button responds normally except the function "historyPressed" never gets executed
I have created a custom annotation view, as described here: https://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/
It works fine and displays the correct data/info.
The problem seems to be that there is no respons to .touchUpInside from the added button.
let button = UIButton()
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
button.frame = CGRect.init(x: 16, y: 543, width: 268, height: 30)
button.backgroundColor = .yellow
calloutView.addSubview(button)
#objc func deleteFromAnnot(sender: UIButton)
{
print("Do something")
}
It looks to me like you have set your button target incorrectly.
Replace
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
With
button.addTarget(self, action: #selector(deleteFromAnnot(sender: )), for: .touchUpInside)
Hi I have tried to align the navigationbar button item position center in the navigation bar. I have changed the navigation bar height programmatically so I have able to align title its working fine, but same I have tried leftbarbutton and rightbarbutton. its has not vertically aligned center. I have tried these programmatically
Title Alignment
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(-15, forBarMetrics: .Default)
RightBarButton:
let button: UIButton = UIButton(type: .Custom)
button.setImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(self.toggleRight), forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 40, 40)
let rightButton = UIBarButtonItem(customView: button)
// rightButton.imageInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = [negativeSpacer ,rightButton ]
LeftBarButton:
let button: UIButton = UIButton(type: .Custom)
button.setImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(self.toggleLeft), forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 40, 40)
let leftButton = UIBarButtonItem(customView: button)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = [negativeSpacer,leftButton ]
Please find the screenshot.
I have a simple + button on the right side to add rows to my tableView, however the button is not appearing. This is how i implement it
let AddButton = UIButton()
AddButton.setTitle("+", forState: .Normal)
let AddView = UIBarButtonItem(customView: AddButton)
self.navigationItem.rightBarButtonItem = AddView
AddButton.addTarget(self, action: #selector(self.addRow), forControlEvents: UIControlEvents.TouchUpInside)
Note, I use the same exact implementation for my hamburger button which does appear. This is the working hamburger button implementation
let navicon = UIButton(type: UIButtonType.System)
navicon.setImage(defaultMenuImage(), forState: UIControlState.Normal)
navicon.frame = CGRectMake(0, 0, 30, 30)
let menu = UIBarButtonItem(customView: navicon)
self.navigationItem.leftBarButtonItem = menu
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
navicon.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), forControlEvents: .TouchUpInside)
why is this happening?
Try:
let AddButton = UIButton(type: .Custom)
AddButton.frame = CGRectMake(0, 0, 44, 44)
I create a UIButton in tvOS via Swift
let randomBtn = UIButton()
randomBtn.setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal, forState: .Normal)
randomBtn.setImage(RndHoover, forState: .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused)
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth - 150, y: 60 , width: 70 , height: 70)
self.view.addSubview(randomBtn)
But the action never get fired if I press the button, is there anything different in tvOS?
func click(sender: UIButton) {
print("click")
}
In tvOS for Button action UIControlEvents TouchUpInside will not call.
You have to use UIControlEvents PrimaryActionTriggered like below.
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
Also Refer this link if you have any confusion
https://forums.developer.apple.com/thread/17925
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.PrimaryActionTriggered)
Solved it, its just a other UIControlEvents