canDisplayBannerAds causes functionality not to work? - ios

So I have an app that on buttonPressed() the apps background view color changes. All worked fine until I added iAds to monetize.
Essentially, calling:
self.canDisplayBannerAds = true
causes the background color to stop changing. When I comment it out it works, and I could just probably comment it out and move on. But I want to understand why it is causing it stop certain functionality. Thanks!
Function to change button color:
view.backgroundColor = getRandomColor()
func getRandomColor() -> UIColor{
var randomRed:CGFloat = CGFloat(drand48())
var randomGreen:CGFloat = CGFloat(drand48())
var randomBlue:CGFloat = CGFloat(drand48())
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}

Try adding this:
originalContentView.backgroundColor = getRandomColor

Related

Is there a way to change all of my IOS App page's Bg Color by using User Defaults?

I am following this tutorial provided on Youtube for: How to Save Data with UserDefaults - Swift
https://www.youtube.com/watch?v=sUhq1vIrRbo
And I have this code that works for one page only and would like to know how to do the same exact thing (changing background color) but for my entire app pages based on the user's choice.
I have tried keeping the checkForStylePreference() in the viewDidLoad()of another page but it did not recognize it. I copy pasted the whole checkForStylePreference() but still other pieces of code were missing. Is the only way to do it is by copy pasting all of the methods of the viewController in all App pages? Or there is a much simpler way as a believe to reduce amount of code? Currently I can change BgColor from white to grey perfectly enter image description here but I don't know how to apply it for all.
This is the code of my NameViewController.swift (the one I've created for the page in the screenshot). Please note that I have 2 more swift files which are SAButton.swift and ConstantStyles.swift (for the colors)
class NameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
nameLbl.text = myString
checkForStylePreference()
}
#IBAction func didChangeStyleSeg(_ sender: UISegmentedControl) {
isDarkMode = sender.selectedSegmentIndex == 1
saveStylePreference()
updateStyle()
}
var myString = String()
#IBOutlet weak var styleSegment: UISegmentedControl!
#IBOutlet weak var nameLbl: UILabel!
var isDarkMode = false
let defaults = UserDefaults.standard
struct Keys {
static let preferDarkMode = "preferDarkMode"
}
func updateStyle(){
UIView.animate(withDuration: 0.4){
// self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
// UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
self.view.backgroundColor = self.isDarkMode ? Colors.lightGrey : .white
//recent correct one
// self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
//self.view.UIBackgroundFetchResult = self.isDarkMode? UIColor.grey : .white
}
}
func saveStylePreference(){
defaults.set(isDarkMode, forKey: Keys.preferDarkMode)
}
func checkForStylePreference(){
let preferDarkMode = defaults.bool(forKey: Keys.preferDarkMode)
if preferDarkMode{
isDarkMode = true
updateStyle()
styleSegment.selectedSegmentIndex = 1
}
}
// Do any additional setup after loading the view.
}
Code of the SAButton.swift
class SAButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupButton()
}
private func setupButton() {
setTitleColor(.white, for: .normal)
backgroundColor = Colors.lightBlue
titleLabel?.font = .boldSystemFont(ofSize: 20)
layer.cornerRadius = frame.size.height / 2
}
}
Code of the ConstantStyles.swift
import UIKit
struct Colors {
static let darkGrey = UIColor(red: 40/255, green: 40/255, blue: 40/255, alpha: 1)
// static let purple = UIColor(red: 212/255, green: 186/255, blue: 86/255, alpha: 1)
static let lightBlue = UIColor(red: 89/255, green: 205/255, blue: 242/255, alpha: 1)
static let darkPurple = UIColor(red: 242/255, green: 232/255, blue: 255/255, alpha: 1.0)
// UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
static let lightPurple = UIColor(red: 240/255, green: 229/255, blue: 255/255, alpha: 1.0)
static let lightGrey = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1.0)
//UIColor(red: 249/255, green: 244/255, blue: 255/255, alpha: 1.0)
}
I believe it could be simple but I am new to Swift, I would like to know what part of code to keep exactly and where. Much appreciated.
Ps: Original project Source Code is provided below the Youtube Video.
You can create a main class and inherit from it
class GeneralVC : UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .red // read color from userdefaults and set it here
}
}
class ViewController: GeneralVC {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Same applies to any UIKit component that you need to affect globally
Another interesting way to do it is to use Appearance:
Perhaps you can use UIViewControllerWrapperView as a parent.
UIView.appearance(whenContainedInInstancesOf: [UIViewControllerWrapperView]) // UIViewControllerWrapperView might be private. In that case it might take some wizardry to get it to work
Another way to do it is to set it when the UITabBarController or UINavigationController presents a new UIViewController. You can do this by subclassing them.
The reason why I don't like subclassing is that you force a subclass for just one simple thing. If you only do it in a few navigation based ones it's much easier and also easier to override with extensions instead of everything through subclassing.

Change viewController BG color based off another view controller

So I need some help for a project. I have a simple tab bar SVC where the first view is a timer and the second is a settings page. On the settings page I've setup a struct with an array of colors, then when a user clicks a button a random color in the array is called and applied to the back ground. This part works just as I Intended. What I'd like to do is then apply that color to the background of the second view.
Here is the settings code
import UIKit
import GameKit
public var randomColor = UIColor()
class SettingsViewController: UIViewController {
#IBOutlet weak var pushMe: UIButton!
let colorProvider = BackgroundColorProvider()
#IBAction func pushMeChange(_ sender: Any) {
randomColor = colorProvider.randomColorBG()
print (randomColor.superclass as Any)
view.backgroundColor = randomColor
}
struct BackgroundColorProvider {
let colors = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), // teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), // yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), // red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), // orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), // dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), // purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), // green color
]
func randomColorBG() -> UIColor {
let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: colors.count)
return colors[randomNumber]
}
}
}
Then I have this in the main viewController I pulled from here:
Changing background color of all views in project from one view controller?
The function does error below doesn't error out however I'm a noob at best, I'm not sure how the bell should work and i doubt its even being called. Any help is appreciated.
// bringing in background color from SettingsViewController
func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
let viewController:SettingsViewController = segue!.destination as! SettingsViewController
viewController.view.backgroundColor = self.view.backgroundColor
}
}
Swift has this neat feature called a singleton that can be used to manage "settings" across a list of view controllers. In the viewDidLoad() method of the timer view you can set the view's background color to singleton.backgroundColor. In the settings view, when the user selects a new color, it should set singleton.backgroundColor = newColorThatUserChose. This way, when the user switches back to the Timer View, the color will automatically switch.
As shown in the link above, a singleton can be created like this:
class Settings {
static let sharedInstance = Settings()
var backgroundColor = UIColor.White // set to white by default.
}
Then in the viewDidLoad method for the Timer View:
self.view.backgroundColor = Settings.sharedInstance.backgroundColor
Finally in the SettingsViewController when the user chooses a new color:
var color = [UIColor.White, UIColor.Black, UIColor.Blue, UIColor.Green........]
Settings.sharedInstance.backgroundColor = color[x] // where x is the index that was chosen.
This will allow the views to change automatically based on the apps settings. To ensure that this works in all of the views that you would like to change the color for,
self.view.backgroundColor = Settings.sharedInstance.backgroundColor
should be placed in every UIViewController.
To further abstract this, you can create a custom class called GeneralUIViewController which is a class of type UIViewController and has the above code in its viewDidLoad method. After doing this every UIViewController should have its class set to GeneralUIViewController. This will make it so you only need to set the background color in one file and every view controller in your project will automatically inherit setting its background color to what the User has chosen in the settings page of this application.
These preferences should probably be saved when the application is reopened so CoreData can be used for this. I'd check out this link for more information.

Function to style TextField borders in swift not working

so I am new to Swift and mobile development, I want to make function that Change TextField style so i don't have to write long code for each TextField i have.
This is what I am trying to do :
func borderstyle(TextField : UITextField){
self.TextField.layer.borderColor = UIColor(red: 46/225, green: 204/225, blue: 113/225, alpha: 1).CGColor;
self.TextField.layer.borderWidth = CGFloat(Float(1.0));
self.TextField.layer.cornerRadius = CGFloat(Float(0.0));
}
I think you can get what i am trying to make form the code , the problem is that TextField in the func is read as #IBOutlet while its not.
I want to style any TextField I have with something like this :
borderstyle(UserNameTextField) // UserNameTextField is #IBOutlet
I know that I am doing some kind of mistake there but I want to know whats the best way to solve that.
Thanks.
Why you pass a textField to the function and then you use self.textField inside of it?
Use:
TextField.layer.borderColor = UIColor(red: 46/225, green: 204/225, blue: 113/225, alpha: 1).CGColor;
TextField.layer.borderWidth = CGFloat(Float(1.0));
TextField.layer.cornerRadius = CGFloat(Float(0.0));
Remove the self pointer from the code.
func borderstyle(textField : UITextField){
textField.layer.borderColor = UIColor(red: 46/225, green: 204/225, blue: 113/225, alpha: 1).CGColor;
textField.layer.borderWidth = CGFloat(Float(1.0));
textField.layer.cornerRadius = CGFloat(Float(0.0));
}
NB: As a good naming concept follow camelCase as shown

Swift ViewController doesn't refresh background color

I have a MainView Controller and a second SettingsView Controller.
In SettingsView controller, I let user select a background color, save it and move back to MainView Controller.
I use Navigation controller segue to move from Main to Settings and use dismissViewControllerAnimated to move back to Main.
My problem is when I set background for main view, it doesn't show up.
But if I close and restart app then it comes up correctly.
Is it because Main view is already open and not refreshed back?
Here is the code:
Settings:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("theme", forKey: "BackgroundColor")
Main:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clearColor()
var backgroundLayer = Util.GetTheme()
backgroundLayer.frame = view.frame
view.layer.insertSublayer(backgroundLayer, atIndex: 0)
}
As per your requirement, you need to move the code for changing color to viewWillAppear method. Since viewDidLoad will be called only once at the time when it get loaded to memory. viewWillAppear will be called each time it will come to foreground or visible to user.
But where you are saving selected color and applying the saved color?
Try This code :
class Colors {
let colorTop = UIColor(red: 192.0/255.0, green: 38.0/255.0, blue: 42.0/255.0, alpha: 1.0).CGColor
let colorBottom = UIColor(red: 35.0/255.0, green: 2.0/255.0, blue: 2.0/255.0, alpha: 1.0).CGColor
let gl: CAGradientLayer
init() {
gl = CAGradientLayer()
gl.colors = [ colorTop, colorBottom]
gl.locations = [ 0.0, 1.0]
}
}

SevenSwitch UI background color in iOS 8

I am using the SevenSwitch control instead of the built in UISwitch. Instead of an on and off mode I want both sides to have the same background color.
I tried the following code:
let mySwitch = SevenSwitch()
let switchTintColor = UIColor(red: 109/255, green: 59/255, blue: 100/255, alpha: 1.0)
mySwitch.offLabel.backgroundColor = switchTintColor
mySwitch.onTintColor = switchTintColor
mySwitch.borderColor = switchTintColor
That gets really close to what I want except for the background behind the thumb view when the switch is off.
You'll want to set the activeColor and inactiveColor properties on the switch. That should get you what you're after.
mySwitch.activeColor = switchTintColor
mySwitch.inactiveColor = switchTintColor

Resources