How to connect UILabel text to UISlider meaning? - swift

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!

Related

Add label in center of imageView

I have the two images like in the picture at the end of the question (the image of a list and a red dot). I want to add a label in the center of the red dot. This is my code that doesn't work:
image = UIImageView(image: UIImage(named: "pallino"))
image.frame = CGRect(x: 55, y: self.view.frame.height-60, width: 22, height: 22)
self.view.addSubview(image)
image.layer.cornerRadius = image.frame.width/2
label = UILabel(frame: CGRect(x: self.image.center.x, y: self.image.center.y, width: image.frame.size.width, height: image.frame.size.height))
label.text = "4"
label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.black
image.addSubview(label)
Can someone tell me were am I wrong?
Problem in this line:
label = UILabel(frame: CGRect(x: self.image.center.x, y: self.image.center.y, width: image.frame.size.width, height: image.frame.size.height))
self.image.center.x - The center point is specified in points in the coordinate system of its superview, it is mean that self.image.center is not center of image
You need frame for label, something like this:
let imageSize = 22
let frame = CGRect(x: 0, y: 0, width: imageSize, height: imageSize)
let label = UILabel(frame: frame)
label.aligment = .center
You can set constraint in your label(centered Horizontally and Vertically). Try with the following code.
let label = UILabel()
label.text = "4"
label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor.black
image.addSubview(label)
label.centerXAnchor.constraint(equalTo: self.image.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: self.image.centerYAnchor).isActive = true
To center the label in UIImageView you can refer to this example which is tested and working solution.
extension UIImageView {
/// Create label programmatically
/// - Returns: UILabel
private func ageSensitiveLabel() -> UILabel {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height))
label.text = "Content"
label.font = .systemFont(ofSize: 5.0)
label.textAlignment = .center
label.numberOfLines = 0
label.minimumScaleFactor = 0.5
label.adjustsFontSizeToFitWidth = true
label.baselineAdjustment = .alignCenters
label.textColor = .white
return label
}
/// Add label as subview to UIImageView
func addAgeSensitiveLabel() {
self.subviews.forEach { view in
DispatchQueue.main.async {
view.removeFromSuperview()
}
}
self.addSubview(ageSensitiveLabel())
}
}
Use it with UIImageView
imageView.addAgeSensitiveLabel()

Swift: SegmentedControl in NavBar with Small TitleView

I am attempting to include a segmentedControl on my navBar that looks like this:
The idea here is that the text "fetching..." is a small titleView. However, my current implementation would result in the text "fetching..." on the lower side like so:
I implement large titles so that I can get two "rows" on the navBar, else the word "fetching..." will be hidden behind the segmentedControl.
Code:
let segmentedControl: UISegmentedControl = {
let items = ["Now","In 15 mins", "In 1 hour"]
let sc = UISegmentedControl(items: items)
sc.selectedSegmentIndex = 0
return sc
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.title = "Back"
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.titleView = segmentedControl
}
Does anyone have any advice?
You can create a customView that holds all the views you want to show in the navigation bar and set that view as titleView like below,
let segmentedControl: UISegmentedControl = {
let items = ["Now","In 15 mins", "In 1 hour"]
let sc = UISegmentedControl(items: items)
sc.selectedSegmentIndex = 0
return sc
}()
let fetchingLabel: UILabel = {
let label = UILabel(frame: .zero)
label.text = "Fetching..."
return label
}()
In viewDidLoad
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 250))
customView.addSubview(segmentedControl)
customView.addSubview(fetchingLabel)
fetchingLabel.frame = CGRect(x: 150, y: 0, width: self.view.frame.width, height: 60)
segmentedControl.frame = CGRect(x: 60, y: 50, width: self.view.frame.width * 0.75, height: 30)
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .automatic
navigationItem.titleView = customView
This should give you below result. You can play with the values to do what you want.

Swift: Setting progress of NSProgressIndicator

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

Swift UITextField icon position set

Add Icon in UITextField using
var leftImageView = UIImageView()
leftImageView.image = leftImage
textField.leftView = leftImageView
textField.leftViewMode = UITextFieldViewMode.Always
leftImageView.frame = CGRectMake(15, 10, 15, 20)
textField.addSubview(leftImageView)
o/p for this
I found solution like remove this code from above code
textField.leftView = leftImageView
It give icon alignment proper but whenever start editing text field text on icon like this
I want o/p like this
First of all, you should definitely not add the image as a subview to the label. It's enough to set the leftView property.
textField.addSubview(leftImageView) // Delete this line
Secondly, any x or y offsets that you apply to the left view's frame are ignored. The text field will only care about the view's size. If you want to add padding around the image, one option is to use a container view and position the image view inside of it.
let leftImageView = UIImageView()
leftImageView.image = leftImage
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRectMake(0, 0, 30, 20)
leftImageView.frame = CGRectMake(0, 0, 15, 20)
textField.leftView = leftView
Another option would be to subclass UITextField and override leftViewRectForBounds.
Referring #hennes syntax , there are some syntax missing in swift like CGRect syntax is changed and userNameTextField.leftViewMode = .always is missing
With swift syntax this worked for me :
let leftImageView = UIImageView()
leftImageView.image = UIImage(named: "email")
let leftView = UIView()
leftView.addSubview(leftImageView)
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20)
userNameTextField.leftViewMode = .always
userNameTextField.leftView = leftView
Try this. May be help you.
var padding: Float = 20
var envelopeView: UIImageView = UIImageView(frame: CGRectMake(padding, 0, 30, 30))
envelopeView.image = UIImage.imageNamed("comment-128.png")
envelopeView.contentMode = UIViewContentModeScaleAspectFit
var viewLeft: UIView = UIView(frame: CGRectMake(padding, 0, 30, 30))
viewLeft.addSubview(envelopeView)
textField.leftView.setFrame(envelopeView.frame)
textField.leftView = viewLeft
textField.leftViewMode = UITextFieldViewModeAlways
var viewRight: UIView = UIView(frame: CGRectMake(textField.frame.size.width - (textField.frame.size.width + 30 + padding), 0, 30, 30))
viewRight.addSubview(envelopeView)
textField.rightView.setFrame(envelopeView.frame)
textField.rightView = viewRight
textField.rightViewMode = UITextFieldViewModeAlways
Swift 3.1
extension UITextField
{
enum Direction
{
case Left
case Right
}
func AddImage(direction:Direction,imageName:String,Frame:CGRect,backgroundColor:UIColor)
{
let View = UIView(frame: Frame)
View.backgroundColor = backgroundColor
let imageView = UIImageView(frame: Frame)
imageView.contentMode = .center
imageView.image = UIImage(named: imageName)
View.addSubview(imageView)
if Direction.Left == direction
{
self.leftViewMode = .always
self.leftView = View
}
else
{
self.rightViewMode = .always
self.rightView = View
}
}
}

swift/ios8: mimick action Sheet behaviour

As it is highly discouraged to use the Action Sheet for overlay controls I am currently trying to mimick it with my own subview but somehow the containing components wont be displayed?
code:
#IBAction func showActionSheet(sender: AnyObject) {
println("SHOW STUFF")
let newView:UIView = UIView()
let myColor:UIColor = UIColor(red:255.0, green:255.0, blue:255.0, alpha:0.6)
newView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
newView.backgroundColor = myColor
view.addSubview(newView)
var pickerFrame: CGRect = CGRectMake(0, self.view.frame.height-(self.view.frame.height+self.view.frame.height-200), self.view.frame.width-20, self.view.frame.height/2)
var picker: UIDatePicker = UIDatePicker(frame: pickerFrame);
picker.backgroundColor = UIColor.whiteColor()
view.addSubview(picker)
var okButton = UIButton()
okButton.titleLabel?.text = "Ok"
okButton.backgroundColor = UIColor.whiteColor()
okButton.frame = CGRectMake(0 , self.view.frame.width-160, self.view.frame.width, 80)
var cancelButton = UIButton()
cancelButton.titleLabel?.text = "Abbrechen"
cancelButton.backgroundColor = UIColor.whiteColor()
cancelButton.frame = CGRectMake(0 , self.view.frame.width-80, self.view.frame.width, 800)
view.addSubview(okButton)
view.addSubview(cancelButton)
let secondView:UIView = UIView()
secondView.frame = CGRectMake(0, 200, self.view.frame.width, self.view.frame.height)
secondView.backgroundColor = UIColor.grayColor()
view.addSubview(secondView)
}
the secondView was just for testing purposes - it will get drawn normally, but somehow my controls wont be
You are not adding the controls to newView the view you placed over the old view. Also the buttons are too big and white. Try this code. This will make them visible. Adjust their height to match the frame of the view and then set the color back. I just changed the background colors to get an idea of the frame. Also call UIButton.setTitle(:forState:) to set the title of the buttons.
#IBAction func showActionSheet(sender: AnyObject) {
println("SHOW STUFF")
let newView:UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
let myColor:UIColor = UIColor(red:255.0, green:255.0, blue:0, alpha:0.6)
newView.backgroundColor = myColor
view.addSubview(newView)
var pickerFrame: CGRect = CGRectMake(0, 0, self.view.frame.width-20, 200)
var picker: UIDatePicker = UIDatePicker(frame: pickerFrame);
picker.backgroundColor = UIColor.redColor()
newView.addSubview(picker)
var okButton = UIButton()
okButton.setTitle("Ok", forState: UIControlState.Normal)
okButton.setTitle("Ok", forState: UIControlState.Highlighted)
okButton.backgroundColor = UIColor.greenColor()
okButton.frame = CGRectMake(0 , self.view.frame.width-160, self.view.frame.width, 80)
var cancelButton = UIButton()
cancelButton.setTitle("Abbrechen", forState: UIControlState.Normal)
cancelButton.setTitle("Abbrechen", forState: UIControlState.Highlighted)
cancelButton.backgroundColor = UIColor.blueColor()
cancelButton.frame = CGRectMake(0 , self.view.frame.width-80, self.view.frame.width, 200)
newView.addSubview(okButton)
newView.addSubview(cancelButton)
}