Toolbar doesn't center the button vertically - swift

Can anyone explain to me why Xcode doesn't vertically center the button when you programmatically add the button to the toolbar? When I work with the toolbar added using the Storyboard, the button appears centered horizontally and vertically.
Toolbar added via a storyboard(looks perfect):
Toolbar added via a code:
My code:
self.navigationController?.isToolbarHidden = false
let testButton = UIButton(type: .custom)
testButton.setTitle("Test", for: .normal)
testButton.addTarget(self, action: #selector(test), for: .touchUpInside)
testButton.backgroundColor = .black
testButton.setTitleColor(.white, for: .normal)
testButton.titleLabel?.font = .boldSystemFont(ofSize: 17)
let tb = UIBarButtonItem(customView: testButton)
self.toolbarItems = [tb]

Related

UIButton doesn't work in child view controller

UIButton gestures doesn't recognize in child view controller.
UIButton add targer work (button get target action)
User interaction is turned on everywhere, button size is okay. I try to set childVC as main, and then all work good, debug view hierarchy said that button view over other elements. IDK where is problem. I can provide more code, just tell me.
Add target code:
view.buttonOfLanguageFromTranslate.addTarget(self, action: #selector(self.openDetailView(_:)), for: .touchDown)
Add child VC code:
let childVC = ChildVC()
view.addSubview(childVC.view)
self.addChild(childVC)
childVC.didMove(toParent: self)
childVC.view.translatesAutoresizingMaskIntoConstraints = false
self.view.isUserInteractionEnabled = true
self.childVC = childVC
Button initialization:
var button: UIButton = {
let button = UIButton()
button.setTitle("Title", for: .normal)
button.setTitleColor(.black, for: .normal)
button.setTitleColor(.green, for: .selected)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
The problem was with
childVC.translatesAutoresizingMaskIntoConstraints = false
(in mainVC)
When i remove it, childVC recognise my tap
and i dont set additional size for childVC, view of childVC is display correct but not childVC. (as i think)

Preventing button image highlight when selected swift 4

I do my swift project using code only no storyboards, so no storyboard solutions please I have an agree to terms button and when selected the checkmark is covered in the tint color I am trying to prevent the tint color highlighting the image when selected I am not sure why this is happening here is the button code
let agreeToTermsAndConditions : UIButton = {
let button = UIButton(type: .system)
button.tintColor = .gray
button.backgroundColor = .white
button.adjustsImageWhenHighlighted = false
button.setImage(UIImage(named:"unchecked" )!, for: .normal)
button.setImage(UIImage(named:"agreechecked" )!, for: .selected)
button.addTarget(self, action: #selector(userAgreedToTerms), for: .touchUpInside)
return button
}()
The code is running on xcode 10.2.1 using swift 4 and iOS 12 deployment
Change
UIButton(type: .system)
to
UIButton(type: .custom)
Now you have complete control over the look and behavior of the button.

UIBarButtonItem contains shopping cart and no. of items

I have been struggling with this for hours. I am new to Swift so all I want to do is add an image of a shopping cart and a string to the .rightBarButtonItem and I cannot get it. So far I have this:
let cartIcon = UIBarButtonItem(image: UIImage(named: "shopping_cart_icon"), style: .plain, target: self, action: #selector(didTapCart(_:)))
let cartTotal = UIBarButtonItem(title: "\(cart_total)", style: .plain, target: self, action: #selector(didTapCart(_:)))
navigationItem.rightBarButtonItems = [cartIcon, cartTotal]
But with this, my icon and number is 50px apart! I also tried creating a custom UIView using this:
let view = UIView()
let button = UIButton(type: .system)
button.layer.shadowRadius = 5.0
// autolayout solution
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: 40).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "shopping_cart"), for: .normal)
button.setTitle("\(cartItemArray.count)", for: .normal)
//button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
But the icon is massive and when I shrink it, the cart total is again really far away. In both cases, the original image asset is green but it takes on the system blue tint upon building. How do I sort this out?
You are really trying to coerce a very standard iOS UIElement into being something totally different. You need to create you own button and place it where the right bar button item is at. Don't make it a barButtonItem. Just your own Button.
Here is a nice answer showing how to do so:
https://stackoverflow.com/a/31912446/793607

Touch image in navigation bar for redirecting another view

I didnt see the image in navigation bar so how can i give event touch inside for that image ? Need help !
This is my code . Remember the image have not in the view.
Detail more for my issue is notification(the bell)
Try and see if this helps.
let img = UIImage(named: "icon.png");
let btn = UIButton(frame: CGRectMake(0,0,(statImg?.size.width)!,(statImg?.size.height)!));
btn.setBackgroundImage(statImg, forState: .Normal);
btn.addTarget(self, action: #selector(YourController.yourMethod), forControlEvents: .TouchUpInside);
let barBtn: UIBarButtonItem = UIBarButtonItem.init(customView: btn);
navigationItem.rightBarButtonItem = barBtn;

How to enable the userinteraction only in the barbutton item of the navigation bar?

I have an application which is having the new Facebook App like UI. In that, a side menu controller is there. It has a button on the navigation bar while clicking on to it will give u the menu. But my problem is in the sample I am using the navigation bar is also can be movable, I fix this by setting userinteractionenabled=no in the navigation bar. But I need to enable the userinteraction only in the buttons in the navigation bar. If I do like what I did the whole navigation bar is became not clickable. Can anybody help me in achieving this?
If you have custom button then use this
UIBarButtonItem *button = self.navigationItem.rightBarButtonItem;
button.enabled = NO;
This is the code I use. It doesn't quite enable user interaction, but I figured out how to set the title white then when clicked change colors. (look at setTitleColor lines
override func viewDidLoad() {
super.viewDidLoad()
let handleButton = UIButton.init(type: .custom)
button.setTitle("Register", for: .normal)
button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
if let button = self.navigationItem.rightBarButtonItem?.customView {
button.frame = CGRect(x:0, y:0, width:80, height:34)
}