can't detect colorScheme change in iOS widget - widget

Can't seem to detect colorScheme change in my iOS widget. I would like to set custom background and foreground colors in my widget but the colorScheme environment is not updating or the widget view WidgetConfiguration isn't being called when colorScheme changes.
The problem can be seen in this simple Widget that is just showing the name of the colorScheme in a Text view and setting a background based on the colorScheme.
Here's the code to illustrate the issue:
#main
struct aioWidget: Widget, MiscellaneousHelper {
#Environment(\.colorScheme) private var colorScheme: ColorScheme
let kind: String = "aioWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
Text(colorScheme == .dark ? "Dark" : "Light")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(colorScheme == .dark ? Color.red : Color.green)
}
.configurationDisplayName("WidgetName")
.description("WidgetDescription")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
}
}
What am I missing? How can I detect colorScheme changes to use custom colors?

Related

Transport bar live label in AVPlayerViewController (tvOS only)

It seems impossible to disable or change the red backgrounded LIVE label in the AVPlayerViewController's transport bar on tvOS 15.2. Also, it is enabled by default ever since 15.2 came out. Here is a picture of the label shown here:
I have tried to play around with some of the available options like this one:
guard let pvc = playerRenderController as? AVPlayerViewController else { return }
pvc.transportBarIncludesTitleView = false
It does hide the LIVE label but it also hides the title (Straight Rhythm) from the image.
Furthermore, the label text seems to be specific to the locale, so in Danish it would be DIREKTE instead of LIVE.
Here is a video that showcases this feature (time 03:08):
Any suggestions on how to hide/change this label?
Hello I found a workaround to do it.
After you play the stream call this function with parameters the playerViewController.view
private func removeLiveLabel(view: UIView) {
let list = ["_AVPlayerViewControllerContainerView", "_AVFocusContainerView", "UIView"]
for subview in view.subviews where list.contains(String(describing: type(of: view))) {
for subSubview in subview.subviews {
if subSubview.value(forKey: "class").debugDescription.contains("_AVxOverlayPlaybackAuxiliaryMetadataView") {
subSubview.removeFromSuperview()
return
}
}
removeLiveLabel(view: subview)
}
}
This function will search the subviews of the AVPlayerViewController object controls and will remove the badge from the view without removing the Title.

Swift iOS -Programmatic RootViewController is Light Gray?

I'm just getting in to programmatic vc's with no more storyboards and I'm following YouTube's LetsBuildThatApp by Brian Voong for guidance https://youtu.be/NJxb7EKXF3U?list=PL0dzCUj1L5JHDWIO3x4wePhD8G4d1Fa6N.
I followed all the directions and for some reason when I launch my app I get this light gray haze over my screen and I can't figure out why? I can faintly see the navigation title and blue background but it's covered by a faded layer.
Step 1:
I deleted my storyboard file and sunder the General Tab under Deployment Info I deleted "Main" from Main Interface.
Step 2:
I changed my ProjectNavigator file to FeedController then Changed the file accordingly
import UIKit
class FeedController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Facebook Feed"
collectionView?.backgroundColor = UIColor.white
}
}
Step 3:
In AppDelegate I added a NavVC and made FeedVC it's root and made the NavVC the Window's root. I also change the NavBar and StatusBar color
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let feedController = FeedController(collectionViewLayout: UICollectionViewFlowLayout())
let navVC = UINavigationController(rootViewController: feedController)
window?.rootViewController = navVC
UINavigationBar.appearance().tintColor = UIColor.blue
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
application.statusBarStyle = .lightContent
return true
}
Step 4:
In info.plist I set View controller-based status bar appearance to NO
I can't figure out why I get this light gray haze over my screen
What am I missing here?
It looks like you are configuring the tintColor instead of the barTintColor. The tintColor changes the color for the navigation buttons and the barTintColor adjusts the navigation bar background color. You can watch this video for more details on customizing navigation bar appearance.
https://www.youtube.com/watch?v=RO8_mqRJO-4

Change UISearchBar textColor not working

I am using google places API for autoComplete widget. I am showing the full screen control in ios 9+ using swift. I am adding a full control as given in the docs.
I have added the code as shown in the docs. Now I want to change the searchBar text color to whiteColor.
So I tried this
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
But I am not getting the desired behaviour. Below is the screenshot
This has been given in Docs
https://developers.google.com/places/ios-api/autocomplete#use_a_table_data_source
But this is not working. I need help with regarding to this.
You need to create an extension like follwoing:
public extension UISearchBar {
public func setNewcolor(color: UIColor) {
let clrChange = subviews.flatMap { $0.subviews }
guard let sc = (clrChange.filter { $0 is UITextField }).first as? UITextField else { return }
sc.textColor = color
}
}
And change color using following code:
controller.searchBar.setNewcolor(UIColor.redColor())
Output is :
Update:
For the change color of searchBar text for the GMSAutocompleteViewController you need to do following code:
let searchBarTextAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
That change the text out put like following image:
And if you wish to change placeholder text and it's color for searchBar. You need to do following code:
let placeholderAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
let attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Find a place", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder
It will be show like:
Use Bellow code for Swift 3
if #available(iOS 9.0, *) {
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.green]
} else {
// Fallback on earlier versions
}
Hope this helps.
For swift 4 ios 12
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes
.updateValue(UIColor.white, forKey: NSAttributedStringKey.foregroundColor.rawValue)
You can use UIAppearance protocol to get the appearance proxy for a class which is available in iOS 5.0 and later.
There are actually two ways to customize appearance for objects and to get the appearance proxy for the class.
To customize the appearance of all instances of a class, use appearance.
To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, use appearanceWhenContainedIn.
You can apply this sample code:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]}];
You can also try another option given in this SO post - UISearchBar text color change in iOS 7.
I hope that covers your issue. Happy coding!
In swift 5:
let searchBarTextAttributes: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 14.0)]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
This is actually very easy, just access the textField inside that searchBar as so:
searchBar.searchTextField.defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
Lastest on swift 5
searchBar.searchTextField.textColor = .white

Default ForegroundColor of StatusBar depending on Theme

I am looking for the default ForegroundColor (or resource key) of the StatusBar in the Light or Dark Theme. I need to set the ForegroundColor manually because it seems to be not updated according to the RequestedTheme.
If I set the RequestedTheme in my app to Light and the SystemTheme is Dark, the StatusBar will be displayed with white ForegroundColor. I expected that the ForegroundColor of the StatusBar depends on the App's RequestedTheme.
The only known way to change the StatusBar colors so far is by doing it in C#. No XAML resources to override unfortunately.
You can change the StatusBar colors like so:
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ForegroundColor = Colors.Black;
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().BackgroundColor = Colors.White;
Windows.UI.ViewManagement.StatusBar.GetForCurrentView() has more properties btw.
If you want to be able to change the colors depending on which theme is requested, you could check for the requested theme:
if( Application.Current.RequestedTheme == ApplicationTheme.Light )
{
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ForegroundColor = Colors.Black;
Windows.UI.ViewManagement.StatusBar.GetForCurrentView().BackgroundColor = Colors.White;
}

TinyMCE removing attribute from custom tag

I'm trying to insert a placeholder in html code that will be replaced later on dynamically. So far I managed to get the code inserted, and TinyMCE recognizes the tag, but when I try to append an id attribute to it, the attribute gets removed for an unknown reason. I tried most of the additional options, but none seem to work.
Current config:
extended_valid_elements : "module[id]",
valid_children : "module[img]",
custom_elements : "module",
The code to create the button (and subsequently insert the code):
setup : function(ed) {
// Add a custom button
ed.addButton("module", {
title : "Module",
image : "images/app-x-php-icon.png",
onclick : function() {
ed.focus();
var options = document.getElementById('rendermcemods').innerHTML+"";
var optionList = options.split('|');
var name=prompt("Please enter module name out of: "+options,optionList[0]);
for(var i=0;i<optionList.length;i++){
if(optionList[i] == name){
var patt=new RegExp('<module id="'+name+'">.*</module>','ig');
var content = '<module id="'+name+'"><img src="images/app-x-php-icon.png" /></module>';
//alert(content);
if(! patt.test(ed.getContent())){
ed.execCommand('mceInsertContent', false,content);
}
}
}
}
});
}
As you might notice, there's an alert before the insert, which I used to verify that the content is right...
When use the button to insert the code and then view the html, this is what I get:
<module><img src=images/app-x-php-icon.png" alt="" /></module>
Would anyone know how to fix this?
Update:
full config settings for tinyMCE:
// General options
mode : "none",
theme : "advanced",
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,\n\
save,advhr,advimage,advlink,emotions,iespell,inlinepopups,\n\
insertdatetime,media,searchreplace,print,contextmenu,paste,\n\
directionality,fullscreen,noneditable,visualchars,\n\
nonbreaking,xhtmlxtras",
// Theme options
theme_advanced_buttons1 : "fullscreen,help,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect,|,module",
theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,|,forecolor,backcolor",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,ltr,rtl,|,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,blockquote,|,insertfile,insertimage",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
// Skin options
skin : "o2k7",
skin_variant : "silver",
document_base_url : "http://www.example.com",
content_css : "content.css",
extended_valid_elements : "module[id]",
valid_children : "module[img]",
/*custom_elements : "module", */
// Drop lists for link/image/media/template dialogs
external_link_list_url : "js/generateList.php?A=link",
external_image_list_url : "js/generateList.php?A=image",
media_external_list_url : "js/generateList.php?A=media",
setup : function(ed) {
// Add a custom button
ed.addButton("module", {
title : "Module",
image : "images/app-x-php-icon.png",
onclick : function() {
ed.focus();
var options = document.getElementById('rendermcemods').innerHTML+"";
var optionList = options.split('|');
var name=prompt("Please enter module name out of: "+options,optionList[0]);
for(var i=0;i<optionList.length;i++){
if(optionList[i] == name){
var patt=new RegExp('<module id="'+name+'">.*</module>','ig');
var content = '<module id="'+name+'"><img src="images/app-x-php-icon.png" /></module>';
//alert(content);
if(! patt.test(ed.getContent())){
ed.execCommand('mceInsertContent', false,content);
}
}
}
}
});
}
Another update: It might be interesting (and hopefully help to solve) to know that the id attribute isn't removed when tinyMCE is loaded and it already is in there, and a clean-up on existing code with the attribute doesn't remove it either.
I would put module to the valid_elements instead of the extended_valid_elements/custom_elements. The extended_valid_elements do sometimes behave strange.
My own config then looks like this (you will need to enlarge your own valid_elements and valid_children settings (if not used in your custom tinymce config you will have to use the defaults (can be found at the moxiecode website))):
// The valid_elements option defines which elements will remain in the edited text when the editor saves.
valid_elements: "#[id|class|title|style|onmouseover]," +
"module," +
"a[name|href|target|title|alt]," +
"#p,blockquote,-ol,-ul,-li,br,img[src|height|width],-sub,-sup,-b,-i,-u," +
"-span[data-mce-type],hr",
valid_children: "body[p|ol|ul|hr]" +
"module[img]" +
",p[a|span|b|i|u|sup|sub|img|hr|#text|blockquote]" +
",span[a|b|i|u|sup|sub|img|#text|blockquote]" +
",a[span|b|i|u|sup|sub|img|#text|blockquote]" +
",b[span|a|i|u|sup|sub|img|#text|blockquote]" +
",i[span|a|b|u|sup|sub|img|#text|blockquote]" +
",sup[span|a|i|b|u|sub|img|#text]" +
",sub[span|a|i|b|u|sup|img|#text]" +
",li[span|a|b|i|u|sup|sub|img|ol|ul|#text]" +
",ol[li]" +
",ul[li]",
The solution I ended up using was modifying the blockElementsMap and the transitional map taht are in the source code. That seemed to be the only way to get the custom tag recognized as 'blocklevel' element, as well as being able to add it exactly like I want in the code for later processing.