Change position of navigationItem.leftBarButtonItem - ios

I have a custom back button created as UIBarButtonItem and inserted to navigationItem.leftBarButtonItem. I need to move it a little bit to the left. I was able to move with an image of UIBarButtonItem, but hit area of the button remained on original place.

Try to create a custom UIButton with the frame you want and after that create the UIBarButtonItem(customView: UIButton) like this for example
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 45, height: 40))
let yourBarButton = UIBarButtonItem(customView: button)

Related

UIbarbutton action is not working after changed the navigation bar position in iOS

I want to change the navigation bar position. After changing the position my back button action is not working. Actually I have added the navigation bar in my custom view controller. I want to change the navigation bar Y position. If I give negative value, back button action is not working and it works for any positive values(0..n). How can I make the back button action after changing the negative values for the navigation bar position.
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: -50, width: bounds.width, height: bounds.height)
self.navigationController?.navigationBar.backgroundColor = UIColor.gray
let button = UIButton(type: .custom)
button.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
button.frame = CGRect(x:0, y:0, width:32, height:32)
button.backgroundColor = UIColor.green
let barButton = UIBarButtonItem(customView: button)
self.navigationController?.navigationItem.backBarButtonItem = barButton

Create UIBarButtonItem with fixed size and with custom icon

I've created navigation bar like picture below
The "drop" button is a custom system UIBarButtonItem. It's contained in Right Bar Button Items and its area was expanded.
I want its size fit the image I set, like Compose button on the right.
public func setCustomButton() {
let btnSearch: UIButton = UIButton(type: .custom)
btnSearch.frame = CGRect(x: 0, y: 0, width: 25.0, height: 25.0)
btnSearch.setImage("Your image", for: .normal)
btnSearch.addTarget(self, action: #selector(yourActionName(_:)), for: .touchUpInside)
et btnBarButtonSearch: UIBarButtonItem = UIBarButtonItem(customView: btnSearch)
navigationItem.rightBarButtonItems = [btnBarButtonSearch]
}
hope, it will help you.

Adding a button on top of googleMapView

I'm trying to add a UiButton on top of googleMapView.
I want to have a another button to open my Google Maps app for navigation from my app.
I tried adding a button programmatically and using xib.. its not working in both ways. (not showing).
Do you guys have any idea about it. I'm using swift
try adding the button as a subview.
create the button like this:
let button = UIButton()
button.backgroundColor = .blue
button.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
button.setTitle("My Button", for: .normal)
and add it as a subview to your googleMapView like this:
googleMapView.addSubview(button)

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)
}

Why don't UIButtons interact (change state to highlighted) when touched?

I always faced that issue and never been able to get the answer. When I create the UIButton programmatically and add to the UI and then when you touch the button that state on the UI not being changed to highlighted/down as the result of interacting with that button.
Another thing, 'sometimes' UIButtons when they are added through the interface builder, they work fine and when you touch them you can see the state in the UI changed to highlighted/down.
Any idea why is this happening?
If your button type is custom you had to set image / color for highlighted state, to differ the state change (From normal to highlighted).
If your button Type is system, you will see the state change effect without changing image / color.
I think you are noticing the difference between UIButton with type Custom vs. System.
I put together this playground to show the difference.
import UIKit
import XCPlayground
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = .whiteColor()
let button1 = UIButton(type: .System)
button1.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
button1.setTitle("One", forState: .Normal)
button1.backgroundColor = .blackColor()
view.addSubview(button1)
let button2 = UIButton()
button2.frame = CGRect(x: 0, y: 60, width: 200, height: 50)
button2.setTitle("Two", forState: .Normal)
button2.backgroundColor = .grayColor()
view.addSubview(button2)
XCPlaygroundPage.currentPage.liveView = view
Please post your programatic button code, so we can investigate more

Resources