How to get all available Chromecast devices? - ios

I want to add an action for a custom Chromecast button.
I can use a default button (GCKUICastButton) with a default action.
let castButton = GCKUICastButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
castButton.tintColor = UIColor.gray
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: castButton)
But I need to create my own view with a list of devices that can connect with Chromecast.
How to get all available Chromecast devices?

We highly recommend that you use the widgets in the Cast SDK for both the button and displaying the list of devices. You should use the various styling options to customize the widgets as needed for your branding.

Related

Swift FontAwesome 5 Pro icons don't show programmatically

In my swift app, I am using the pro version fontawesome. I got the name
Font Awesome 5 Pro
== FontAwesome5Pro-Light
and define it in a button as
var button = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
button.titleLabel?.font = UIFont(name: "FontAwesome5Pro-Light", size: 20)
button.setAttributedTitle(NSMutableAttributedString(string: "\u{f13d}"), for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
"\u{f13d}" is the unicode anchor icon Cheatsheet for FontAwesome
But the icon just comes out with the question mark in the square. If I create a button in the storyboard, set the font, create an outlet and then set the unicode from code behind it displays correctly.
I'm confused as to the cause. Any ideas?
Thanks ^.^
Setting the attributed title cancels the titleLabel?.font. Set the font of the attributed string.

iOS13+ Creating a constant button on top of every screen

I maintain an app SDK that contains a button which should always be displayed on top of all screens in the application, but this functionality broke with iOS 13+ which seems to have changed some internals. The button disappears after changing from the first screen.
// My code that does not work anymore
var applicationWindow: UIWindow? = UIApplication.shared.keyWindow
...
button.frame = CGRect(x: 0, y: 0, width: 94, height: 73)
applicationWindow!.addSubview(button)
I saw similar posts on SOF suggesting code like
UIApplication.shared.windows.first(where: { $0.isKeyWindow })? but it doesn't work for me, and I'm struggling to create and manage a second UIWindow, any code sample would be really appreciated as I'm only a maintainer and I don't work actively on iOS anymore.

Add labels to navigation bar

I am adding a calendar to my app and thought it would be cool to mimic the format of standard iOS Calendar app as per weekday labels:
In the Calendar app, these labels (S,M,T,W,T,F,S) seem to be integrated into the navigation bar, so I was wondering if there is a way to implement this or if this is something Apple left to themselves (as there seems to be no standard way to add anything but bar button items). Mind you, these labels should be dynamic - e.g. rearrange in case of day 2 as firstWeekDay for certain Locale.
Apple proposes not to resize navigationBar itself, but remove shadow from bar and add custom view under your navigationBar.
Please refer the apple recommended approach for extended navigation bars here - https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007418-Intro-DontLinkElementID_2
And your particular scenario - https://developer.apple.com/library/content/samplecode/NavBar/Listings/NavBar_ExtendedNavBar_ExtendedNavBarViewController_swift.html#//apple_ref/doc/uid/DTS40007418-NavBar_ExtendedNavBar_ExtendedNavBarViewController_swift-DontLinkElementID_13
You just need to set a view to your title view of navigationItem for example:
sampleLabel.textAlignment = .center
sampleLabel.textColor = UIColor.white
sampleLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
sampleLabel.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
sampleLabel.text = "T"
let titleView = UIView
titleView.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
titleView.addSubview(sampleLabel)
navigationItem.titleView = titleView
You can add labels to your navigationBar like sampleLabel wherever you want.

How to programmaticaly add button in iOS game app swift 3?

Can someone explain the code for a programatically added button in an iOS game application for swift 3 Xcode 8? All the other threads on this topic we're in single view and didn't work for me. I couldn't figure out how to add buttons to the game app Main.storyboard, so I'm trying to make a programattically added button. This is the code I'm trying to use now but doesn't work:
var playAgain = UIButton()
playAgain.setTitle("Play Again", for: .normal)
playAgain.setTitleColor(UIColor.black, for: .normal)
playAgain.backgroundColor = SKColor.green
playAgain.layer.borderWidth = 2
playAgain.layer.cornerRadius = 18
playAgain.frame = CGRect(x: (self.frame.width)/2, y: (self.frame.height)/2, width: 100, height: 36)
self.view?.addSubview(playAgain)
Why would the buttons in single view be different in game apps? Also, when(and if) this is created, how would I modify the Touches ended method to know when the button was touched?
Your code is adding the button to the .view, but using the coordinate system of the SKScene. So, your button is there, just not in view.
Assuming you want the button to be centered on the screen (at least, for now), change the placement to:
playAgain.frame = CGRect(x: 0, y: 0, width: 100, height: 36)
playAgain.center = (self.view?.center)!
self.view?.addSubview(playAgain)
This will put the button above the game scene (z-layer, that is), so you can use normal button tap without needing to deal with touches. So, right after you add the button:
self.view?.addSubview(playAgain)
playAgain.addTarget(self, action: #selector(playAgainTapped(_:)), for: .touchUpInside)
and then elsewhere in your class:
func playAgainTapped(_ sender: Any?) -> Void {
print("Play again was Tapped!")
// take whatever action you want here
}
There is another way to create a button programmatically. You can create an empty UIView and override touch method. Also you are able to process touch event on this view and simulate buttons action. I think this is a fastest way for you.

iOS dynamic UI in simple terms?

This may be too basic or require rephrasing. I am in the process of learning Swift and iOS programming and have developed a basic application that runs successfully on my iPhone 5. The app consists of a label, a button, and a UIImageView. It looks the way I want it to on my iPhone 5.
I figured most of this out by just playing around and so I am creating all these elements programatically. The code looks like this:
let banner = UILabel(frame: CGRect(x: 10, y: 10, width: 300.0, height: 75.0))
let button = UIButton(frame: CGRect(x: 45, y: 75, width: 235.0, height: 60.0))
let imageView = UIImageView(frame: CGRect(x: 22, y: 150, width: 280.0, height: 410.0))
And then I configure them in viewDidLoad to make them show stuff.
Now the question...how to I make them all the right size when running on different devices? I can load up the app on my iPad Mini but it's all scrunched over to the left of the view. So I need to do some kind of dynamic layout but not sure where to start.
All help appreciated!
Rather than creating the view sizes explicitly with initWithFrame: constructors, you can programmatically create and NSLayoutConstraints to your views to automatically layout your views, the same as if you used Auto Layout with the Interface Builder. See Apple's Auto Layout Guide for more details.

Resources