How to Add label On the top of the View Swift - ios

Here is what I am trying to do:
The screenshot is taken from iPhone:
Code Below:
//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)
//Show HighLight Lable
let highLightLabelCount = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
Question: How to set Frame or constraints label setup at top Left Corner?
Any help would be greatly appreciated.
Thanks in advance.

Your constraint should be centered x,y not top left with
//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)
//Show HighLight Lable
let highLightLabelCount = UILabel()
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
highLightLabelCount.translatesAutoresizingMaskIntoConstraints = false
self.highLightView.textAlignment = .center
NSLayoutConstraint.activate([
self.highLightLabelCount.centerXAnchor.constraint(equalTo: self.highLightView.centerXAnchor),
self.highLightLabelCount.centerYAnchor.constraint(equalTo: self.highLightView.centerYAnchor)
])

I think this will be useful if you would like to pin label to top left corner of your highlight view.
NSLayoutConstraint.activate([
highLightView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
highLightView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
highLightLabelCount.leadingAnchor.constraint(equalTo: view.leadingAnchor),
highLightLabelCount.bottomAnchor.constraint(equalTo: highLightView.topAnchor))
])

Related

UITextField - bottom line programmatically

I am trying to add a bottom line to UITextField ...
what am I doing wrong?
let emialTextField: UITextField = {
let textField = UITextField()
textField.layer.cornerRadius = 5
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
Try below code.
override func viewDidLoad() {
super.viewDidLoad()
let emialTextField: UITextField = {
let textField = UITextField(frame: CGRect(x: 100, y: 90, width: 100, height: 30))
textField.borderStyle = .none
textField.placeholder = "emial adress"
let bottomline = CALayer()
bottomline.frame = CGRect(x: 0, y: textField.frame.height + 1, width: textField.frame.width, height: 2)
bottomline.backgroundColor = UIColor.init(red: 3/255, green: 4/255, blue: 5/255, alpha: 1).cgColor
textField.layer.addSublayer(bottomline)
return textField
}()
view.addSubview(emialTextField)
emialTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 100).isActive = true
emialTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 90).isActive = true
emialTextField.heightAnchor.constraint(equalToConstant: 30).isActive = true
emialTextField.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
It seems textField.frame.width is zero when executing below line of your code:
bottomline.frame = CGRect(x: 0, y: textField.frame.height - 2, width: textField.frame.width, height: 2)
So you should add bottomline layer to your text field after size set for emialTextField via constraint or frame size.

How to add subview so that it overlaps another view?

I have the following subview
webView = WKWebView(frame: CGRect(x: 3, y: horizontalLine.frame.maxY + 8, width: self.view.frame.width-6, height: CGFloat(self.view.frame.height - 150)))
webView.navigationDelegate = self
self.view.addSubview(webView)
I want to add the following subview on top of it (not inside it).
textSizeChangeView = UIView(frame: CGRect(x: 0, y: textBtn.frame.maxY, width: self.view.frame.width, height: 100))
textSizeChangeView.backgroundColor = UIColor(red: 0.97, green: 0.98, blue: 0.98, alpha: 1.00)
let mySlider = UISlider(frame:CGRect(x: 10, y: 0, width: self.view.frame.width - 20, height: 30))
self.textSizeChangeView.addSubview(mySlider)
mySlider.center = self.textSizeChangeView.center
mySlider.minimumValue = 1
mySlider.maximumValue = 2
mySlider.isContinuous = true
mySlider.tintColor = UIColor(red: 0.33, green: 0.79, blue: 0.93, alpha: 1.00)
textSizeChangeView.isHidden = true
mySlider.addTarget(self, action: #selector(self.sliderValueChanged(sender:)), for: .valueChanged)
self.view.addSubview(self.textSizeChangeView)
How can I do this?
The order of adding subviews matters.
If you want to make sure that textSizeChangeView will be on top of webView in case of overlapping then you need to make sure that self.view.addSubview(webView) is called before self.view.addSubview(self.textSizeChangeView).

CALayer not showing text in

I am trying to add CALayer in my view, layer is shown but text is hide.
My code is as follow:
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 25, height: 25))
label.text = "5"
label.font = .boldSystemFont(ofSize: 10)
label.adjustsFontSizeToFitWidth = true
label.textColor = #colorLiteral(red: 0.4392156899, green: 0.01176470611, blue: 0.1921568662, alpha: 1)
label.textAlignment = .center
let sublayer = label.layer
sublayer.backgroundColor = UIColor.yellow.cgColor
sublayer.borderColor = UIColor.red.cgColor
self.view.layer.addSublayer(sublayer)
Can you please let me know where I am going wrong?
Thank you,

Center a UILabel of a UIView

I am currently trying to make a simple pop up, here I have a button, once it gets tapped.
It should show a simple view above it, I achieved the blue background, but I am not being able to add a label to it, and center the label to the blue popup
let width = (sender as! UIButton).frame.width
let y = (sender as! UIButton).frame.origin.y
let x = (sender as! UIButton).frame.origin.x
myView = UIView(frame: CGRect(x: x, y: y - 30, width:width, height: 30))
myView.backgroundColor = UIColor.blue
let label = UILabel()
label.text = (sender as! UIButton).titleLabel?.text
label.font = UIFont(name:"avenirNext-Meduim",size:20)
label.center = self.myView.center
myview.addSubview(label)
self.view.addSubview(myView)
UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations: {
self.myView.alpha = 0.0
}) { (finished: Bool) in
self.myView.isHidden = true
}
Try to set your label frame:
let label = UILabel(frame: CGRect(origin: .zero, size: yourLabelSize))
Instead of
label.center = self.myView.center
try
label.centerXAnchor.constraint(equalTo: myView.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: myView.centerYAnchor).isActive = true
after
myview.addSubview(label)
self.view.addSubview(myView)
My solution
let codedLabel:UILabel = UILabel()
codedLabel.frame = CGRect(x: myView.center.x, y: myView.center.y, width: 51, height: 30)
codedLabel.textAlignment = .center
codedLabel.text = "q"
codedLabel.numberOfLines = 1
codedLabel.textColor=UIColor.red
codedLabel.font=UIFont.systemFont(ofSize: 22)
codedLabel.backgroundColor=UIColor.lightGray
self.myView.addSubview(codedLabel)
codedLabel.translatesAutoresizingMaskIntoConstraints = false
codedLabel.heightAnchor.constraint(equalToConstant: 30).isActive = true
codedLabel.widthAnchor.constraint(equalToConstant: 30).isActive = true
codedLabel.centerXAnchor.constraint(equalTo: codedLabel.superview!.centerXAnchor).isActive = true
codedLabel.centerYAnchor.constraint(equalTo: codedLabel.superview!.centerYAnchor).isActive = true
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
let label = UILabel()
label.frame = CGRect.init(x: headerView.center.x-60, y:headerView.center.y-15, width: 120, height: 30)
label.textAlignment = .center
label.textColor = #colorLiteral(red: 0.1290470958, green: 0.2743584514, blue: 0.6418049335, alpha: 1)
label.font = UIFont(name: "Roboto-Bold", size: 15)
label.text = "11-12-2020"
headerView.addSubview(label)
headerView.backgroundColor = #colorLiteral(red: 0.9967060685, green: 0.9998621345, blue: 0.9997505546, alpha: 1)

Make a UIView stick to bottom of the ViewController frame in Swift

I have created a UIView (it's just a blue rectangle) and I want it to stick to the bottom of the frame/view of the screen.
var nextBar = UIView()
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: 600, width: self.view.frame.width, height: 50)
self.view.addSubview(nextBar)
I'm assuming i need to add something in after the 'y:' part in the CGRect, but i can't seem to make it work or find anywhere that shows me this in swift.
Thanks in advance
hmmm try this...
override func viewDidLoad() {
super.viewDidLoad()
let theHeight = view.frame.size.height //grabs the height of your view
var nextBar = UIView()
nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5)
nextBar.frame = CGRect(x: 0, y: theHeight - 50 , width: self.view.frame.width, height: 50)
self.view.addSubview(nextBar)
}
for the 'y:' in CRect, you're taking the size, in height, of the screen size and you're subtracting how many ever pixels you want. It will appear the same for what ever screen size.
You can do it programmatically using constraints
fileprivate func setupName(){
let height = CGFloat(50)
lblName.text = "Hello world"
lblName.backgroundColor = .lightGray
//Step 1
lblName.translatesAutoresizingMaskIntoConstraints = false
//Step 2
self.view.addSubview(lblName)
//Step 3
NSLayoutConstraint.activate([
lblName.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
lblName.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
lblName.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,constant: -height),
lblName.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
])
}
Please refer link for more details
Like this:
view.addConstraintsWithFormat(format: "V:[v0(50)]|", views: nextBar)

Resources