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)
Related
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)
I need to create a "burger menu" button for my app on my navigationController's left side but since the NavCon is transparent I need to have a shadow on my icon.
Thus I've created a custom UIButton with an image, a drop shadow and the added it as a custom view on a UIBarButtonItem as follows:
let menuButton = UIButton(type: .custom)
menuButton.addTarget(self, action: #selector(showSideMenu), for: .touchUpInside)
menuButton.setImage(#imageLiteral(resourceName: "menu_white"), for: .normal)
menuButton.tintColor = UIColor.white
menuButton.layer.masksToBounds = false
menuButton.layer.shadowColor = UIColor.black.cgColor
menuButton.layer.shadowOpacity = 1.0
menuButton.layer.shadowRadius = 5
menuButton.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: menuButton)
The code above works perfectly fine on iOS 11, but when I tested my app on ios 9 and 10 (both simulators and real devices) the menu icon is invisible. It is clickable and works as expected, but there is no visible icon.
In the View Hierarchy Debugger I can see a UIButton with 0 width and height, while in ios 11 I can see the normal UIButtonBarStackview with the embedded UIButton.
Any ideas on how to fix this and why this is happening? Thank you very much!
Please mention the button frame
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 40))
It may helps to you thank you
You can just call menuButton.sizeToFit() and it will work.
This question already has answers here:
Set image and title for bar button item?
(3 answers)
Closed 5 years ago.
Basically I want to achieve this using UIBarButton:
I tried using setTitle but I end up having something like this:
Create your custom navigation bar - add UIView to top of your viewcontroller, and add UIButton in left corner of it. And label underneath button.
Can be done by using custom view with UIBarButtonItem. Something like this.
Swift
let image = UIImage.init(named: "image_name")
let button = UIButton.init(type: .custom)
button.frame = CGRect.init(x: 0, y: 0, width: 100, height: 40)
button.setBackgroundImage(image, for: .normal)
button.setTitle("title", for: .normal)
let barButtonItem = UIBarButtonItem.init(customView: button)
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)
}
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