I am having a problem showing the StatusBar in my UITableViewController because it does not have a background. So I want to be able to do that programatically. I have seen this in apps like Facebook and Youtube. Can anyone help me with that?
In order to add a view behind status bar, you can use below code which is written for Swift 3
You need to create an extension for it and you can use this in any view controller where it is required.
let SCREEN_WIDTH = UIScreen.main.bounds.size.width
extension UIViewController {
func addStatusBarBackgroundView(viewController: UIViewController) -> Void {
let rect = CGRect(origin: CGPoint(x: 0, y: 0), size:CGSize(width: SCREEN_WIDTH, height:20))
let view : UIView = UIView.init(frame: rect)
view.backgroundColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 1) //Replace value with your required background color
viewController.view?.addSubview(view)
}
}
and simply call this by writing one line in your view controller:
override func viewDidLoad() {
super.viewDidLoad()
addStatusBarBackgroundView(viewController: self)
//Your extra code
}
Happy Coding..!!
To let your UINavigationBar stretch behind you status bar, you can implement the function position(for:) on the navigation bar's delegate.
Swift3 Example
class MyViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navigationBar.delegate = self
}
}
extension MyViewController: UINavigationBarDelegate {
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}
Related
I am trying to work with delegate and protocol but met a problem.
I created 2 buttons on one ViewController and created 1 imageView in another ShowViewController. The color of the imageView will change according to which button is pressed.
ViewController.swift
import UIKit
protocol getColorProtocol {
func getColor(color:String?)
}
class ViewController: UIViewController {
var color: String?
var delegate:getColorProtocol?
#IBAction func blueButtonPressed(_ sender: Any) {
color = "blue"
delegate?.getColor(color: color)
}
#IBAction func redButtonPressed(_ sender: Any) {
color = "red"
delegate?.getColor(color: color)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
ShowViewController.swift
import UIKit
class ShowViewController: UIViewController, getColorProtocol {
var viewOne = ViewController()
#IBOutlet weak var colorView: UIImageView!
func getColor(color: String?) {
print("color is \(color!)")
if color == "red"{
colorView.backgroundColor = UIColor(displayP3Red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
}
else if color == "blue" {
colorView.backgroundColor = UIColor(displayP3Red: 0/255, green: 0/255, blue: 255/255, alpha: 1)
}
}
override func viewDidLoad() {
super.viewDidLoad()
viewOne.delegate = self
}
}
press here to view the storyboard!
You may check my storyboard above.
Currently, it has no error but it cannot call the getColor function. I am wondering if the problem is coming from the delegate?.getColor(color: color) statement.
Any ideas?
First of all, in your case, you don't need to use delegate pattern
Your ViewController connect directly to ShowViewController through segue
You can use prepareForSegue instead
Secondly, I don't think you understand how delegate pattern works,
Inside ShowViewController, you're create a new ViewController called viewOne, it isn't the original ViewController so that's why your code doesn't work as expected
As I mentioned above, you should use segue
first, go to the storyboard setup your segue identifier. Then, inside your ViewController
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "goToShowVC") {
//your code here
}
}
You don't need a delegate for this, but just prepare for segue. I see that you have two segues set up already, so in your viewcontroller, assuming your segues are called "blue" and "red" in your storyboard, you could add:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? ShowViewController {
if segue.identifier == "blue" {
destinationVC.color = UIColor.blue
} else if segue.identifier == “red” {
destinationVC.color = UIColor.red
}
}
}
then you would want to add a color variable to manipulate in your showviewcontroller. For example:
var color = UIColor.white
colorview.backgroundcolor = color
I am trying to apply the colour picker from the question below.
Simple swift color picker popover (iOS)
The way I set this up was the colour picker class is attached to a UIView within the main view controller. The code by Michael Ros works but when I try to access it using my main view controller nothing happens. Below is the code I use in my view controller. Is this correct? I went over other questions and I am not sure what I am doing wrong.
class ViewController: UIViewController {
weak var colorPickerDelegate: ColorPickerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.colorPickerDelegate = self
}
}
}
extension ViewController: colorPickerDelegate {
func colorChanged(color: UIColor) {
print(color)
}
}
The color picker code can be found on the attached question as I wasn't sure if it was allowed to copy the code over.
Thanks for the help
You should sublcass UIView and assign it to the view controllers view, then set the delegate of the ColorPickerView to the view controller:
ColorPickerView.swift
class ColorPickerView : UIView {
weak var delegate: ColorPickerDelegate?
// Other code from Michael Ros's adaptation.
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
lazy var colorPickerView = ColorPickerView(frame: CGRect(origin: .zero, size: CGSize(width: self.view.frame.width, height: 300)))
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.colorPickerView.delegate = self
self.view.addSubview(colorPickerView)
}
}
extension ViewController: ColorPickerDelegate {
func colorDidChange(color: UIColor) {
print(color)
}
}
you should replace
colorChanged -> colorDidChange !
if you delegate is ColorPickerDelegate; I think so
I am using the library CVCalendar for Swift and I am trying to customize the appearance of the calendar.
I have a ViewController called "FirstViewController" where the UIView for the Calendar is located. In my file FirstViewController.swift I managed to implement the property customizations where CVCalendar provides a function as API.
But I don't know what is the correct way to customize other properties which do not have these function APIs. For example the CVCalendar's class CVCalendarViewAppearanceDelegate has many properties with default values that can be modified such as dayLabelWeekdayInTextColor.
Question: How do I modify correctly a property like the "dayLabelWeekdayInTextColor from FirstViewController.swift?
Note that I created a function setupCalendarFormat() where I am modifying the properties; I tried calling it in viewDidLoad() and viewDidLayoutSubviews() but the modifications are not working correctly.
My code below:
import UIKit
import CVCalendar
class FirstViewController: UIViewController {
#IBOutlet weak var containerView: UIView!
#IBOutlet weak var menuView: CVCalendarMenuView!
#IBOutlet weak var calendarView: CVCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
//self.view.layoutIfNeeded()
setupCalendarFormat()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//setupCalendarFormat()
menuView.commitMenuViewUpdate()
calendarView.commitCalendarViewUpdate()
}
func setupCalendarFormat(){
//CircleView background color and alpha
calendarView.appearance.dayLabelPresentWeekdayHighlightedBackgroundColor = UIColor.black //not working highlight color not changing to black
//day numbers' color
calendarView.appearance.dayLabelWeekdayInTextColor = UIColor.green // not working correctly, color is only changing to green after i select the number
calendarView.appearance.dayLabelPresentWeekdayTextColor = UIColor.green // working , probably becuase by default the present day is selected
}
}
extension FirstViewController: CVCalendarViewDelegate, CVCalendarMenuViewDelegate{
//All these APIs are succesfully called
func presentationMode() -> CalendarMode {
return .weekView
}
func firstWeekday() -> Weekday {
return .sunday
}
func dayOfWeekFont() -> UIFont {
return UIFont.systemFont(ofSize: 12.0, weight: .medium)
}
func dayOfWeekTextColor() -> UIColor {
return .black
}
func dayOfWeekTextUppercase() -> Bool {
return false
}
func shouldShowWeekdaysOut() -> Bool {
return true
}
}
The CVCalendar repository provides you with a CVCalendar Demo that demonstrates how to set the colors of the calendar elements.
It basically works like this:
Set a view controller as the delegate of the CVCalendar
Make the delegate conform to the delegate protocol
Add the delegate methods for setting the colors of the calendar element
When the calendar is about to display for example a text element, it asks its delegate (your view controller) for the color by calling the specific method that will - based on your logic - return a UIColor, e.g.:
extension ViewController: CVCalendarViewDelegate, CVCalendarMenuViewDelegate {
func dayOfWeekTextColor(by weekday: Weekday) -> UIColor {
return weekday == .sunday ? UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0) : UIColor.white
}
}
See the demo implementation.
It is working for navigationBar:
var colour = UIColor.red
self.navigationController?.navigationBar.tintColor = colour
But do not work for toolbar:
self.navigationController?.toolbar.tintColor = colour
I searched the internet and stack overflow. No answer is workable for me.
Some people said:
self.toolbar.barTintColor = UIColor.redColor()
It is also not working for me. (value of type 'thisView' has no member 'toolbar')
I want to edit the toolbar color in coding. No change in the storyboard setting. Thanks.
EDIT:
I am working on adding a toolbar under the webview. Like go back, stop, reload.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
var colour = UIColor.red
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = colour
// problem in here ..........................
self.navigationController?.toolbar.tintColor = UIColor.black
let URL = NSURL(string: "https://www.apple.com")
webView.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)
}
#IBAction func backButton(_ sender: AnyObject) {
webView.goBack()
}
#IBAction func nextButton(_ sender: AnyObject) {
webView.goForward()
}
#IBAction func refreshButton(_ sender: AnyObject) {
webView.reload()
}
#IBAction func stopbutton(_ sender: AnyObject) {
webView.stopLoading()
}
}
If you've just got a toolbar that is on a ViewController in Your Storyboard all you need to do is add an IBOutlet to your View Controller and connect the toolbar in the storyboard to that outlet. This code goes in your ViewController
#IBOutlet var toolbar: UIToolbar?
Then, in the storyboard, hold the control button and click drag from View Controller (in the left sidebar) to your toolbar. This will create a connection between the toolbar in the storyboard to the toolbar var in your code. After that connection is made all you need to do is set the barTintColor on that toolbar variable like so:
self.toolbar.barTintColor = UIColor.blue
I wrote this function a few days ago to kinda workaround the fact that you can't change the color.
func setStatusBarColor(){
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 81/255, green: 184/255, blue: 222/255, alpha: 1)
self.view.addSubview(view)
}
Also make sure to set the statusbar to .lightContent
UIApplication.sharedApplication().statusBarStyle = .LightContent
Easy code
class PreferencesTabBar: UITabBar {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
self.backgroundColor = UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 0.5)
}
}
I set the class of the UITabBar in IB to my custom class, but the background doesn't become the color that I wanted, but instead it becomes completely transparent, it's like there is no UITabBar at all (of course excepts the UITabBarItems I can still see them)
One solution is to set the background of your tab bar from the view controller that holds your tab bar, by creating a #IBOutlet (ctrl-drag) from the tab bar.
// ViewController.swift
class ViewController: UIViewController {
#IBOutlet weak var myTabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTabBar.backgroundColor = UIColor(red: 166.0/255.0, green: 142.0/255.0, blue: 83.0/255.0, alpha: 0.5)
}
// ...
}