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)
}
}
Related
I need to set constraints for a UILabel, but whenever I try to add a constraint for my UITableViewCell it messes up, and the element goes to the top left corner it seems
Here's how I set up my UI elements
Here's how it looks like when I run in a simulator
"Food" is the "English Meaning" UILabel
Here's my code for the cell file that's generated with the xib
class ReusableCell: UITableViewCell {
#IBOutlet weak var arabicMeaning: UILabel!
#IBOutlet weak var englishMeaning: UILabel!
#IBOutlet weak var upvoteButton: UIButton!
#IBOutlet weak var downvoteButton: UIButton!
#IBOutlet weak var upvoteCount: UILabel!
#IBOutlet weak var downvoteCount: UILabel!
var upFlag = true
var downFlag = true
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
static func nib() -> UINib{
return UINib(nibName: K.ReusableCell, bundle: nil)
}
#IBAction func ReviewPressed(_ sender: UIButton) {
if upFlag && downFlag{
if sender == upvoteButton{
upvoteCount.text = String(Int(upvoteCount.text!)! + 1)
}
else {
downvoteCount.text = String(Int(downvoteCount.text!)! + 1)
}
}
else if sender == upvoteButton && upFlag{
upFlag = false
downFlag = true
upvoteCount.text = String(Int(upvoteCount.text!)! + 1)
}
else if sender == downvoteButton && downFlag{
upFlag = true
downFlag = false
downvoteCount.text = String(Int(downvoteCount.text!)! + 1)
}
}
}
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
I have prepared the app and I am stuck at adding the code to turn the ringer silent. When I press the button a notification is sent but I want to turn the ringer volume to 0. This is my code. ALSO I AM CONFUSED WHERE TO PUT THE CODE.
import UIKit
import MapKit
protocol AddViewControllerDelegate {
func addViewController(controller: AddViewController, didAddCoordinate coordinate: CLLocationCoordinate2D, radius: Double, identifier: String, note: String, eventType: EventType)
}
class AddViewController: UITableViewController {
#IBOutlet var addButton: UIBarButtonItem!
#IBOutlet var zoomButton: UIBarButtonItem!
#IBOutlet weak var eventTypeSegmentedControl: UISegmentedControl!
#IBOutlet weak var radiusTextField: UITextField!
#IBOutlet weak var noteTextField: UITextField!
#IBOutlet weak var mapView: MKMapView!
var delegate: AddViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItems = [addButton, zoomButton]
addButton.isEnabled = false
}
#IBAction func textFieldEditingChanged(sender: UITextField) {
addButton.isEnabled = !radiusTextField.text!.isEmpty && !noteTextField.text!.isEmpty
}
#IBAction func onCancel(sender: AnyObject) {
dismiss(animated: true, completion: nil)
}
#IBAction private func onAdd(sender: AnyObject) {
let coordinate = mapView.centerCoordinate
let radius = Double(radiusTextField.text!) ?? 0
let identifier = NSUUID().uuidString
let note = noteTextField.text
let eventType: EventType = (eventTypeSegmentedControl.selectedSegmentIndex == 0) ? .onEntry : .onExit
delegate?.addViewController(controller: self, didAddCoordinate: coordinate, radius: radius, identifier: identifier, note: note!, eventType: eventType)
}
#IBAction private func onZoomToCurrentLocation(sender: AnyObject) {
mapView.zoomToUserLocation()
}
}
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)")
}
Just learning so I am sure there are much better ways to code what I have so far. Anyways, I have a simple task list that is saved to core data. You can segue to another VC where you can edit or create new task. I wanted to be able to put a uiswitch in the custom cell that would allow you to set the task as complete and then reload the tableview. I have searched all over and found this Select UITableView's row when tapping on its UISwitch in Swift but can't seem to translate a solution. Any help would be great.
enter image description here
I am not sure if I am supposed to code the save within the custom cell or just or pass delegate.
Here is my custom cell
protocol SettingCellDelegate : class {
func didChangeSwitchState(sender: TaskCell, isOn: Bool)
}
class TaskCell: UITableViewCell {
var delegate: SettingCellDelegate!
#IBOutlet weak var taskImage: UIImageView!
#IBOutlet weak var arrowImage: UIImageView!
#IBOutlet weak var dueDateLabel: UILabel!
#IBOutlet weak var dueDateValue: UILabel!
#IBOutlet weak var dueTimeLabel: UILabel!
#IBOutlet weak var dueTimeValue: UILabel!
#IBOutlet var taskCellStatus: UITextField!
#IBOutlet var taskCellSwitchDisplay: UISwitch!
#IBAction func taskCellSwitchAction(sender: AnyObject) {
if (taskCellSwitchDisplay.on) {
taskCellStatus.text = "Completed"
} else {
taskCellStatus.text = "Active"
}
}
#IBOutlet weak var taskNameValue: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
taskCellSwitchDisplay.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
}
func configureCell(task: Tasks) {
taskImage.image = UIImage(named: "Icon.png")
arrowImage.image = UIImage(named: "arrow_filled.png")
dueDateLabel.text = "Due Date:"
dueDateValue.text = task.taskDate
dueTimeLabel.text = "Due Time:"
dueTimeValue.text = task.taskTime
taskNameValue.text = task.taskName
taskCellStatus.text = task.status
//taskCellStatus.hidden = true
if (taskCellStatus.text == "Completed") {
taskCellSwitchDisplay.on = true
} else {
taskCellSwitchDisplay.on = false
}
}
func switchChanged(taskCellSwitchDisplay: UISwitch) {
_ = taskCellSwitchDisplay.on
if taskCellSwitchDisplay.on == true {
let switchValue = "Completed"
print("Switch Variable: \(switchValue)")
delegate.didChangeSwitchState(self, isOn: true)
} else {
let switchValue = "Active"
print("Switch Variable: \(switchValue)")
delegate.didChangeSwitchState(self, isOn: false)
}
}
Here is where I am getting the delegate in my tableview
extension TaskCell: SettingCellDelegate {
func didChangeSwitchState(sender: TaskCell, isOn: Bool) {
if isOn == true {
print("true")
} else {
print("false")
}
}