Swift: Setting progress of NSProgressIndicator - swift

I have tried to setup my own NSProgressIndicator with the following method.
class ProgressViewController : NSViewController
{
override func loadView()
{
let view = NSView(frame: NSMakeRect(0,0,300,120))
view.wantsLayer = true
view.layer?.borderWidth = 0
self.view = view
let text = NSTextField(frame: NSRect(x: 20, y: 45, width: 260, height: 20))
text.drawsBackground = true
text.isBordered = false
text.backgroundColor = NSColor.controlColor
text.isEditable = false
self.view.addSubview(text)
text.stringValue = "Progress Text"
let indicator = NSProgressIndicator(frame: NSRect(x: 20, y: 20, width: 260, height: 20))
indicator.minValue = 0.0
indicator.maxValue = 100.0
indicator.doubleValue = 33.0
self.view.addSubview(indicator)
}
}
My problem is that the indicator is always shown full. Am I missing a trick?

The default style is indeterminate. Set it to false and everything should be OK:
indicator.isIndeterminate = false

Related

How to add activity Indicator to center of webView

With this code I get indicator but to top left corner of webView and behind borders
(I have DetailView storyboard but want to write code programmatically)
var webView = WKWebView()
var detailItem : CountriesFinal?
var spinner = UIActivityIndicatorView()
override func viewDidLoad() {
super.viewDidLoad()
spinner.frame = CGRect(x: webView.frame.width / 2, y: webView.frame.height / 2, width: 10, height: 10)
webView.addSubview(spinner)
spinner.startAnimating()
spinner.hidesWhenStopped = true
webView.backgroundColor = .blue
webView.translatesAutoresizingMaskIntoConstraints = false
webView.navigationDelegate = self
webView.frame = CGRect(x: 125, y: 150, width: 250, height: 150)
webView.layer.borderWidth = 5
webView.layer.borderColor = UIColor.red.cgColor
webView.contentMode = .scaleAspectFit
webView.backgroundColor = .blue
self.view.addSubview(webView)
I notice that this line doesn't change anything and I thought it may be the solution:
spinner.frame = CGRect(x: webView.frame.width / 2, y: webView.frame.height / 2, width: 10, height: 10)
I've tried to set translatesAutoresizingMaskIntoConstraints of spinner to false but then whole webView disappear with purple warning " Layout Issues: Position and size are ambiguous for WKWebView."
UPDATED CODE(webVIew not showing):
webView.frame = CGRect(x: 125, y: 150, width: 250, height: 150)
webView.backgroundColor = .blue
webView.translatesAutoresizingMaskIntoConstraints = false
webView.navigationDelegate = self
webView.layer.borderWidth = 5
webView.layer.borderColor = UIColor.red.cgColor
webView.contentMode = .scaleAspectFit
webView.backgroundColor = .blue
self.view.addSubview(webView)
self.view.insertSubview(spinner, aboveSubview: webView)
spinner.centerXAnchor.constraint(equalTo: webView.centerXAnchor).isActive = true
spinner.centerYAnchor.constraint(equalTo: webView.centerYAnchor).isActive = true
spinner.startAnimating()
spinner.hidesWhenStopped = true
In my opinion WKWebView is one of the system views that should probably be a "leaf" in your view hiercarchy. It is very complex in the content it manages and may not like it if you add subviews.
Instead of putting the spinner inside the WKWebView, I would probably put it into a view that is a sibling and stack it in front of the WebView in the z-order. It will float above the web view, but look like it is inside of it.
Here's a Playground I threw together that demonstrates the idea:
import UIKit
import WebKit
import PlaygroundSupport
NSSetUncaughtExceptionHandler { error in
debugPrint(error)
}
let controller = UIViewController()
let webView = WKWebView()
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.load(URLRequest(url: URL(string: "https://www.apple.com")!))
let spinner = UIActivityIndicatorView()
spinner.translatesAutoresizingMaskIntoConstraints = false
controller.view.addSubview(webView)
webView.frame = controller.view.bounds
controller.view.insertSubview(spinner, aboveSubview: webView)
spinner.centerXAnchor.constraint(equalTo: webView.centerXAnchor).isActive = true
spinner.centerYAnchor.constraint(equalTo: webView.centerYAnchor).isActive = true
PlaygroundSupport.PlaygroundPage.current.liveView = controller
spinner.startAnimating()
Working code. Changed place of addSubview
webView.frame = CGRect(x: 125, y: 150, width: 250, height: 150)
webView.backgroundColor = .blue
webView.translatesAutoresizingMaskIntoConstraints = false
webView.layer.borderWidth = 5
webView.layer.borderColor = UIColor.red.cgColor
webView.contentMode = .scaleAspectFit
webView.backgroundColor = .blue
self.view.addSubview(webView)
self.webView.addSubview(self.spinner)//first we need to add subview then make center or something
spinner.center = CGPoint(x: webView.bounds.width / 2, y: webView.bounds.height / 2)
webView.navigationDelegate = self
spinner.startAnimating()
spinner.hidesWhenStopped = true

How to connect UILabel text to UISlider meaning?

I'm new in Swift and trying to create Temperature Converter from ºC to ºF programmatically. But I have no idea how to change meaning of my label when l drag the slider.
I tried to change addTarget, but it didn't help. I really have no idea what's wrong and why my label still being "0ºC".
enter image description here
Here is my code:
class ListVC: UIViewController {
var cLabel : UILabel = UILabel()
var slider : UISlider = UISlider()
override func viewDidLoad() {
super.viewDidLoad()
**// creating label**
let cLabel = UILabel()
cLabel.frame = CGRect(x: 150, y: 250, width: 100, height: 200)
cLabel.textAlignment = .center
cLabel.text = "0ºC"
cLabel.textColor = .black
cLabel.font = UIFont(name: "Apple SD Gothic Neo", size: 25)
self.view.addSubview(cLabel)
self.view = view
**// creating slider**
let slider = UISlider()
slider.frame = CGRect(x: 110, y: 300, width: self.view.frame.width, height: 250)
slider.center = self.view.center
slider.translatesAutoresizingMaskIntoConstraints = false
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 0
slider.isContinuous = true
slider.isUserInteractionEnabled = true
slider.addTarget(self, action:#selector(sliderValue(sender:)), for: .valueChanged)
self.view.addSubview(slider)
}
**// creating function that had to change label value but something went wrong**
#objc func sliderValue(sender: UISlider) {
cLabel.text = String(sender.value)
}
Thank you!

A programmatically UISlider not changing width

My swift code is all code no storyboard. My slider sx should change the width of the imagview when the value is changed. That is not happening no change is visible on the imageview. The imageview should increase / decrease when the slider value is changed.
import UIKit
class ViewController: UIViewController {
var sx = UISlider()
var pic = UIImageView()
var ww = 80
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[sx,pic].forEach{
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
$0.backgroundColor = UIColor.systemPink
}
pic.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: CGFloat(ww), height: 50)
sx.frame = CGRect(x: view.center.x-115, y: view.center.y + 200, width: 80, height: 50)
sx.addTarget(self, action: #selector(ji), for: .valueChanged)
}
#objc func ji(sender : UISlider){
ww = Int(sx.value)
}
}
Reset frame inside ji function as code inside viewDidLoad won't be triggered again it's called only once when the vc is loaded
#objc func ji(sender : UISlider){
ww = Int(sx.value)! * 80
pic.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: CGFloat(ww), height: 50)
}

Edit- how can I get UIlabel to get in the middle of the container?

how can i get UIlabel to get in the middle of the container? and that text and view disappear equally and come to the next view.
EDIT: This code is working fine for me now
class TestViewController: UIViewController {
let container = UIView()
let redSquare = UIView()
let blueSquare = UIView()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.label.textColor = UIColor.orange
self.label.font = label.font.withSize(25)
self.label.numberOfLines = 0
self.label.center = self.blueSquare.center
self.label.textAlignment = NSTextAlignment.center
self.label.text =
"sosdihfiosdfhsdhfdisfhsdfhdsoifhsdofhsdifhdsofhdsofhsdohdsfdosdohdfh"
self.blueSquare.addSubview(label)
self.label.frame = CGRect(x: 45, y: 120, width: 300, height: 100)
self.container.frame = CGRect(x: 7, y: 200, width: 400, height: 500)
self.view.addSubview(container)
self.redSquare.frame = CGRect(x: 0, y: 0, width: 400, height: 500)
self.blueSquare.frame = redSquare.frame
self.redSquare.backgroundColor = UIColor.darkGray
self.blueSquare.backgroundColor = UIColor.darkGray
self.container.addSubview(self.redSquare)
}
Define two labels like:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
then inside viewDidLoad() before adding redSquare to container fix some labels's property and add label's to blue and red square:
label.center = self.redSquare.center
label.textAlignment = NSTextAlignment.center
label.text = "same text"
label2.textAlignment = NSTextAlignment.center
label2.center = self.blueSquare.center
label2.text = "same text"
self.blueSquare.addSubview(label2)
self.redSquare.addSubview(label)

Cannot hide view after animation

I am creating an animation which brings an image and a label from the left to the centre of the view.
imageLogo.isHidden = true is not hidden when App is run
labelLogo is not shown at all on the view
I have been reading tutorials, but I just don't see what is wrong with my code.
let paymentLogo = UIImage(named: "paymentImage")
var imageLogo:UIImageView!
var overlayView = UIView()
var logoAppeared:Bool!
let labelLogo = UILabel()
override func viewDidLayoutSubviews() {
//move picture off the screen here
self.imageLogo = UIImageView(image:paymentLogo)
imageLogo.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imageLogo.center.x -= 400
self.view.addSubview(imageLogo)
self.labelLogo.frame =
CGRect(x: 0, y: 0, width: 200, height: 21)
self.labelLogo.center.x -= 400
self.labelLogo.text = "Booking Completed"
self.labelLogo.textAlignment = .center
self.view.addSubview(labelLogo)
}
override func viewDidAppear(_ animated: Bool) {
UIView.animate(withDuration: 2.0, delay: 0.1, options: [], animations: {
//animate paymentCompletedLogo
self.overlayView = UIView(frame: self.view.frame)
self.overlayView.backgroundColor = UIColor.black
self.overlayView.alpha = 0.4
//animate labelLogo
self.labelLogo.frame = CGRect(x: self.view.center.x, y: 90, width: 200, height: 21)
self.labelLogo.backgroundColor = UIColor.gray
self.labelLogo.text = "Booking Completed"
self.labelLogo.textColor = .black
self.labelLogo.textAlignment = .center
//animate imageLogo
self.imageLogo.frame =
CGRect(x: self.view.center.x,y: self.view.center.y,width: 100,height: 100)
self.view.addSubview(self.overlayView)
self.view.addSubview(self.imageLogo)
self.view.addSubview(self.labelLogo)
}) { finished in
self.overlayView.isHidden = true
self.imageLogo.isHidden = true //it is not hidden in simulator
self.logoAppeared = true
}
}
I tried removing following your code from animation method it worked fine.
You are trying to add subview in animation method. imageLogo and labelLogo is all ready added in view.
self.view.addSubview(self.overlayView)
self.view.addSubview(self.imageLogo)
self.view.addSubview(self.labelLogo)