Swift - label updates with SwiftRangeSlider - ios

I am trying to work with SwiftRangeSlider implemented by BrianCorbin:
https://github.com/BrianCorbin/SwiftRangeSlider
#IBOutlet weak var labelForRangeSlider: UILabel!
#IBOutlet weak var rangeSlider: RangeSlider!
#IBOutlet weak var submitButton: UIButton!
override func viewDidLayoutSubviews() {
rangeSlider.updateLayerFrames()
}
...
And this shows:
I want to update "Time", which is labelForRangeSlider, as rangeSlider's lowerValue and upperValue change.

You are supposed to add action for the event .valueChanged for SwiftRangeSlider:
override func viewDidLoad() {
super.viewDidLoad()
let rangeSlider: RangeSlider = RangeSlider(frame: CGRect(x: 0, y: 0, width: 200, height: 60))
rangeSlider.addTarget(self, action: #selector(self.rangeSliderValueChanged(slider:)), for: UIControlEvents.valueChanged)
self.view.addSubview(rangeSlider)
}
#IBAction func rangeSliderValueChanged(slider: RangeSlider) {
print(slider.lowerValue)
}

Connect the IBAction below to the range slider in your storyboard and than you can change your label when you move the slider. You need to check lowerValue and upperValue of your slider.
#IBAction func rangeSliderValuesChanged(_ rangeSlider: slider) {
labelForRangeSlider.text "Min: \(slider.lowerValue), Max: (slider.upperValue)")
}

Related

Why button changes after the click?

I want to set a custom font to UIButton. I need to set it programmatically because the font is not applied otherwise. The problem is that it changes back to the built-in font after the click. What am I doing wrong?
import UIKit
class LoginViewController: UIViewController {
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var eyeButton: UIButton!
#IBOutlet weak var loginButton: UIButton!
var iconClick = true
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
loginButton.titleLabel?.font = UIFont(name: "Capitana-Bold", size: CGFloat(16))
}
#IBAction func iconAction(sender: AnyObject) {
if (iconClick == true) {
passwordTextField.isSecureTextEntry = false
eyeButton.setImage(UIImage(named: "eye-closed"), for: .normal)
} else {
passwordTextField.isSecureTextEntry = true
eyeButton.setImage(UIImage(named: "eye-open"), for: .normal)
}
iconClick = !iconClick
}
#IBAction func onLoginClicked(_ sender: Any) {
}
}
In iOS 15 you need to use configuration for button. This should work:
loginButton.configuration?.attributedTitle?.font = UIFont(name: "Capitana-Bold", size: CGFloat(16))
This is an iOS 15 Filled button. You cannot configure it by saying
loginButton.titleLabel?.font = ...
That was possible (but wrong) in iOS 14 and before, but with an iOS 15 button it is impossible. To configure a Filled button programmatically, you must set its configuration object.
https://developer.apple.com/documentation/uikit/uibutton/configuration
In particular you want to set the button configuration attributed title.
https://developer.apple.com/documentation/uikit/uibutton/configuration/3750779-attributedtitle

Unable to load a svg image from url in ios app

I am trying to display svg images from remote url. I have used SwiftSVG library to display the image. But instead of image a round circle is displaying. Please check screenshot:
I am using following code to display the image in UITableViewCell:
import UIKit
import SwiftSVG
class AssetsTableViewCell: UITableViewCell {
#IBOutlet weak var assetNameLabel: UILabel!
#IBOutlet weak var assetSymbolLabel: UILabel!
#IBOutlet weak var assetAveragePrice: UILabel!
#IBOutlet weak var assetImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
func set(cryptoCurrency: CryptoCurrency) {
let svgURL = URL(string: cryptoCurrency.icon)!
let hammock = UIView(SVGURL: svgURL) { (svgLayer) in
svgLayer.fillColor = UIColor(red:0.52, green:0.16, blue:0.32, alpha:1.00).cgColor
svgLayer.resizeToFit(CGRect(x: 10, y: 10, width: 60, height: 60))
}
addSubview(hammock)
assetImageView.loadImageUsingCache(withUrl: cryptoCurrency.icon)
assetNameLabel.text = cryptoCurrency.name
assetSymbolLabel.text = cryptoCurrency.symbol
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}

Importing animation function from custom swift file failed

I was creating animations for buttons using extension in swift but when i call the animation function it generates error.
import UIKit
extension UIButton{
func wiggle() {
let wiggleAnim = CABasicAnimation(keyPath: "psoition")
wiggleAnim.duration = 0.05
wiggleAnim.repeatCount = 5
wiggleAnim.autoreverses = true
wiggleAnim.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
wiggleAnim.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
layer.add(wiggleAnim, forKey: "position")
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var colorizeBtn: UIButton!
#IBOutlet weak var wiggleBtn: UIButton!
#IBOutlet weak var dimBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func colorizeBtnWasPressed(_ sender: Any) {
}
#IBAction func wiggleBtnWasPressed(_ sender: Any) {
wiggleBtn.wiggle()
}
#IBAction func dimBtnwasPressed(_ sender: Any) {
}
}
View Controller
Your wiggleBtn is of type UIView but you write an extension to UIButton.
Either change the extension to UIView or change the type of wiggleBtn to UIButton.

Resize a UIView inside a stackview

I need help to accomplish this:
Its a group of 3 photos. The first photo is the button screen before the click, the second photo is the screen after the click and the third photo is the way i've designed in the IB using stackview
I've being trying to create this and this is the result i've got so far. I still haven't created anything as the After button click image shows because of this:
My Result now
As you can see in the gif when i press the button, the UIView height constraint.constant is set to a higher value and the UIView get higher. All i want is the stripped background to get higher as well.
This is the way the view is disposed in IB.
And finally, this is the way i've coded
class LetterScreenViewController: UIViewController, ResizeWordViewDelegate {
#IBOutlet weak var youTubeView: YouTubeView!
#IBOutlet weak var phonemeView: PhonemeView!
#IBOutlet weak var wordView: WordView!
var letterPresenter: LetterPresenter?
#IBOutlet weak var heightWordViewConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
delegateWordObj()
if let presenter = letterPresenter {
presenter.setupView()
}
}
#IBAction func dismissLetter(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
func delegateWordObj() {
wordView.resizeWordViewDelegate = self
}
func didPressButton() {
if heightWordViewConstraint.constant <= 0 {
heightWordViewConstraint.constant = 30
}else{
heightWordViewConstraint.constant = 0
}
}
}
import UIKit
protocol ResizeWordViewDelegate {
func didPressButton()
}
class WordView: UIView {
#IBOutlet weak var backgroundListras: UIImageView!
#IBOutlet var WordView: UIView!
#IBOutlet weak var showWordButton: UIButton!
var resizeWordViewDelegate: ResizeWordViewDelegate!
override init(frame: CGRect) {
super.init(frame: frame)
xibInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibInit()
}
func xibInit() {
Bundle.main.loadNibNamed("WordView", owner: self, options: nil)
addSubview(WordView)
WordView.frame = self.bounds
WordView.round(corners: [.topLeft,.topRight], radius: 120)
WordView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
#IBAction func showWordButtonAction(_ sender: Any) {
resizeWordViewDelegate.didPressButton()
self.frame.size = CGSize(width: 375, height: 200)
self.backgroundListras.frame.size = CGSize(width: 375, height: 200)
}
}
I want to know a way to resize this UIView with all the content in it. After finding a solution to increase the height i'll be able to put all the others components shown when i press the button.
I believe that your goal is to create something like this:
All I had to do was change the height of the constraint programmatically
Check the solution in the project below:
https://gitlab.com/DanielLimaDF/ResizeTest.git
Important: Beware of addSubview, it can remove constraints from a View if the constraints were created before calling addSubview

Programmatically change slider value and update the Label

I have 3 sliders as shown above:
And when the "Min tip" it's equal or greater than "Default tip", I add 1 to "Default tip". And the same logic it's for Default to Max tip.
So, until now I have this code, that works partially because the Default tip's slider grows in the UI, but the label isn't updated because the delegate isn't called.
#IBOutlet weak var minLabel: UILabel!
#IBOutlet weak var defaultLabel: UILabel!
#IBOutlet weak var maxLabel: UILabel!
#IBOutlet weak var minSlider: UISlider!
#IBOutlet weak var defaultSlider: UISlider!
#IBOutlet weak var maxSlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
minSlider.addTarget(self, action: #selector(SettingsViewController.minSliderValueChanged(_:)), forControlEvents: UIControlEvents.AllEvents)
defaultSlider.addTarget(self, action: #selector(SettingsViewController.defaultSliderValueChanged(_:)), forControlEvents: UIControlEvents.AllEvents)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func minSliderValueChanged(sender: UISlider) {
let valueMin = Int(minSlider.value)
let valueDefault = Int(defaultSlider.value)
_ = Int(maxSlider.value)
minLabel.text = "Min tip: \(valueMin)%"
if(valueMin >= valueDefault){
defaultSlider.value = Float(valueMin+1)
}
}
#IBAction func defaultSliderValueChanged(sender: UISlider) {
let value = Int(defaultSlider.value)
defaultLabel.text = "Default tip: \(value)%"
}
#IBAction func maxSliderValueChanged(sender: UISlider) {
let value = Int(maxSlider.value)
maxLabel.text = "Max tip: \(value)%"
}
What I'm missing?
After defaultSlider.value = Float(valueMin+1) update the text of the label, like so:
defaultLabel.text = "Default tip: \(valueMin+1)%"
Slider values range from 0 (slider at left end) to 1 (slider at right end).
By doing let valueMin = Int(minSlider.value), variable valueMin can either be 0, or 1. I assume this isn't what you want really.
Perhaps this is closer to what you're looking for?
#IBAction func minSliderValueChanged(sender: UISlider) {
let valueMin = minSlider.value
let valueDefault = defaultSlider.value
_ = Int(maxSlider.value)
minLabel.text = "Min tip: \(Int(valueMin*100))%"
if(valueMin >= valueDefault){
defaultSlider.value = valueMin
}
}

Resources