UIButton title not displaying - ios

I just started developing in Swift.
I created an UIButton dynamically and added it to the View.
My issue is the view is not displaying the button.
My Code:
var button:UIButton = UIButton();
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50);
button.setTitle("Midhun", forState: UIControlState.Normal);
self.view.addSubview(button);
Then I added image to the button and then it is showing:
button.setImage(UIImage(named: "step_first.jpg"), forState: UIControlState.Normal);
I couldn't figure out what is happening. Please help

In case you subclass UIButton and override its layoutSubviews(), make sure you call super.layoutSubviews() inside

For me the issue was that I wasn't using the setter methods.
I was doing notificationsButton.titleLabel?.text = "x" instead of notificationsButton.setTitle("x", forState: UIControlState.Normal)

I actually figured out the issue.
In Swift the title of UIButton is white.
I changed the background color of parent View, now it is showing.
self.view.backgroundColor = UIColor.black

Related

UIBarButtonItem with custom UIButton is invisible on iOS <= 10

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.

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)

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

Swift / How to set addSubview to Subview?

I want to add a Subview on my googleAdBanner-Subview. (as a custom close UIButton).
If I add the UIButton as subview to self.view.addSubview(btn), it is working. But since it may take a while to load an Ad, sometimes the UIButton is visible even tho the googleAdBanner is still invisible.
If I add UIButton as subview of googleAdBanner, the UIButton will not be displayed.
override func viewDidLoad() {
super.viewDidLoad()
let googleAdBanner = GADBannerView(frame:CGRectMake(0,self.view.frame.size.height - 50,self.view.frame.size.width,50))
googleAdBanner.adUnitID = "ca-app-pub-xx"
googleAdBanner.rootViewController = self
googleAdBanner.loadRequest(GADRequest())
self.view.addSubview(googleAdBanner)
let btn: UIButton = UIButton(frame: CGRectMake(self.view.frame.size.width - 25, self.view.frame.size.height - 50, 25, 25))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(RootVC.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1
googleAdBanner.addSubview(btn)
}
What am I missing? Help is very appreciated.
make your 'btn' sth like: let btn: UIButton = UIButton(frame: CGRectMake(0,0, btnWidth, btnHeight)) after adding that as subview to the banner, then adjust the button frame either by setting the CGrect or auto layout

Make the navigationbar title clickable swift

I would like to make the navigationbar title to be clickable. When user click on it, it should perform a segue. But I have no idea how to do this.
I have tried the following to get the title and apply the Tap gesture to it.
var subviews = self.navigationController?.navigationBar.subviews
if let subviews = subviews {
// Better check for array length before accessing to the 1st element
var subview = subviews [0]
}
but its giving me error Variable subview inferred to have type AvyObject, which may be unexpected
One more approach to add button as a title of navigation controller.
You need to set navigation item title view to your button object
Create button object in viewDidLoad() method:
Swift 4.0 Edit
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = .red
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(clickOnButton), for: .touchUpInside)
navigationItem.titleView = button
Here you can see in last line button is directly set to the titleView of navigationItem which will add button at the center of navigation bar.
Action method for button is below:
#objc func clickOnButton() {
}
Kampai's answer updated for Swift 3:
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.clickOnButton), for: .touchUpInside)
self.navigationItem.titleView = button
To get rid of that error, specify a type for your if let constant. I'd also recommend changing that if let constant name since it's the same as the one already declared in the previous line.
var subviews = self.navigationController?.navigationBar.subviews
if let subviewArray:NSArray = subviews {
// Better check for array length before accessing to the 1st element
var subview:UILabel = subviewArray[0] // <-- If the subview's a UILabel
}

Resources