Swift - set position - ios

How I can set the position of the colored bar to the bottom of the screen?
I have these lines:
let ground = SKSpriteNode(color: UIColor(red: 144/255, green: 100/255,
blue: 144/255, alpha: 1.0) , size: CGSize(width: view.frame.size.width, height: 20))
ground.position = ???
addChild(ground)

This should work:
let ground = SKSpriteNode(color: UIColor(red: 144/255, green: 100/255,
blue: 144/255, alpha: 1.0) , size: CGSize(width: self.size.width, height: 20))
ground.anchorPoint = CGPoint(x: 0, y: 0)
ground.position = CGPoint(x: 0,y: 0)
addChild(ground)
Possible pitfalls:
You must reposition if an orientation change happens. Also make sure that the scene is not bigger as the screen.

Try with these lines of code:
let ground = SKSpriteNode(color: UIColor(red: 144/255, green: 100/255,
blue: 144/255, alpha: 1.0) , size: CGSize(width: view.frame.size.width, height: 20))
view.addSubview(ground)
ground.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
ground.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true
ground.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true
ground.heightAnchor.constraintEqualToConstant(40).active = true

Related

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).

auto layout and bottom line textfield

I've set constraints for these UITextField like this
But when I custom the textfield it's gone over the constraint
emailTextField.backgroundColor = UIColor.clear
emailTextField.attributedPlaceholder = NSAttributedString(string: emailTextField.placeholder!, attributes: [NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)])
emailTextField.borderStyle = .none
emailTextField.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
let bottomLayer = CALayer()
bottomLayer.frame = CGRect(x: 0, y: 20, width: 1000, height: 0.7)
bottomLayer.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
emailTextField.layer.addSublayer(bottomLayer)
Run..
Please tell me why? Many thanks for that...
You have set CALayer width 1000. It should be width of your TextField.
Change below code:
Add below code to get updated frame of emailTextField and set the width of Layer:
emailTextField.setNeedsLayout()
emailTextField.layoutIfNeeded()
bottomLayer.frame = CGRect(x: 0, y: 20, width: emailTextField.frame.size.width, height: 0.7)
UPDATE
If you want to reuse this textField configuration you can create one function like below:
func setupTextField(textField: UITextField) {
textField.backgroundColor = UIColor.clear
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder!, attributes: [NSAttributedString.Key.foregroundColor: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)])
textField.borderStyle = .none
textField.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
textField.setNeedsLayout()
textField.layoutIfNeeded()
let bottomLayer = CALayer()
bottomLayer.frame = CGRect(x: 0, y: 20, width: textField.frame.size.width, height: 0.7)
bottomLayer.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
textField.layer.addSublayer(bottomLayer)
}
You can call above method this way:
setupTextField(textField: textField)

Weird line sticking out on the end of UILabel

I have a UILabel on each section of my tableview. I have this line sticking out on the end of the label and this is coming from an iPhone 6. I've encountered it before on the iPhone 5 and managed to make it disappear by changing the width of the UILabel.
Here's the code on viewForHeaderInSection:
let view = UIView(frame: CGRectMake(0, 0, self.view.bounds.width, 30))
view.backgroundColor = UIColor(red: 248/255, green: 247/255, blue: 243/255, alpha: 1.0)
let label = UILabel(frame: CGRectMake((self.view.bounds.width - (self.view.bounds.width * 0.4)) / 2, 0, self.view.bounds.width * 0.4, 25))
label.backgroundColor = UIColor(red: 248/255, green: 247/255, blue: 243/255, alpha: 1.0)
label.textColor = UIColor(red: 163/255, green: 144/255, blue: 99/255, alpha: 1.0)
label.textAlignment = .Center
label.text = "YOUR TITLE HERE"
let line1 = UIView(frame: CGRectMake((self.view.bounds.width - (self.view.bounds.width * 0.8)) / 2, 13, self.view.bounds.width * 0.8, 1))
line1.backgroundColor = UIColor(red: 163/255, green: 144/255, blue: 99/255, alpha: 1.0)
view.addSubview(line1) // Adds the line first
view.addSubview(label) // Then the label on top of the line for the look.
return view

Add Gradient on a view in Swift

The following code seems to work using Objective-C however its Swift version doesn't work.
I have tried the following:
Add the gradient inside the collection view cell
Add the gradient in the viewController
Use UIColor & CGColor
Use insertSubLayer
var mGradient = CAGradientLayer()
mGradient.frame = favoriteCell.mImageView.bounds
var colors = [CGColor]()
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor)
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 0).CGColor)
mGradient.startPoint = CGPointMake(0.1, 0.5)
mGradient.endPoint = CGPointMake(0.9, 0.5)
favoriteCell.mImageView.layer.addSublayer(mGradient)
Swift 3
let mGradient = CAGradientLayer()
mGradient.frame = favoriteCell.mImageView.bounds
var colors = [CGColor]()
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor)
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor)
mGradient.startPoint = CGPoint(x: 0.1, y: 0.5)
mGradient.endPoint = CGPoint(x: 0.9, y: 0.5)
mGradient.colors = colors
favoriteCell.mImageView.layer.addSublayer(mGradient)
You need to set set the colors of the gradient
mGradient.colors = colors
Try this one:
var mGradient : CAGradientLayer = CAGradientLayer()
mGradient.frame = favoriteCell.mImageView.bounds
mGradient.frame.origin = CGPointMake(0.0,0.0)
var colors = [CGColor]()
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor)
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 0).CGColor)
mGradient.locations = [0.0 , 1.0]
mGradient.startPoint = CGPointMake(0.1, 0.5)
mGradient.endPoint = CGPointMake(0.9, 0.5)
mGradient.frame = CGRect(x: 0.0, y: 0.0, width: favoriteCell.mImageView.frame.size.width, height: favoriteCell.mImageView.frame.size.height)
favoriteCell.mImageView.layer.insertSublayer(mGradient, atIndex: 0)

Move coordinates of UIView to LLO with Swift

I would expect this code to move my newLabel from the upper left corner (ULO) to the lower left corner (LLO) but it doesn't! Apparently I need someone to hold my hand through the process of setting (0, 0) to LLO for a UIView. I've been through the documentation and read post after post on this forum but can't seem to make it happen. Disclaimer: I am trying to accomplish this in a playground in xCode 6 beta 2 so it possible it's a bug or not supported.
var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
var myView = UIView(frame: CGRectMake(0, 0, 200, 100))
myView.layer.borderWidth = 2
myView.addSubview(newLabel)
myView.transform = CGAffineTransformMakeScale(1, -1)
myView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
It seems like you haven't added Myview in main view try this code:--
var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
var myView = UIView(frame: CGRectMake(0, 0, 200, 100))
myView.layer.borderWidth = 2
myView.addSubview(newLabel)
myView.transform = CGAffineTransformMakeScale(1, -1)
myView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
self.view.addSubview(myView);
To flip the UIView it has to be a subView of another view... My issue was I was trying to flip the mainView or baseView which apparently cant be done. This make since, since it needs a graphic space to be able to draw to. Below is the code achieving what I was looking for. Also of not I also had to "flip" my label as well to return it to it upright state (again this is logical).
var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.transform = CGAffineTransformMakeScale(1, -1)
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
var subView = UIView(frame: CGRectMake(50, 50, 200, 100))
subView.transform = CGAffineTransformMakeScale(1, -1)
subView.layer.borderWidth = 2
subView.addSubview(newLabel)
subView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
var myView = UIView()
myView = UIView(frame: CGRectMake(0, 0, 300, 200))
//myView.transform = CGAffineTransformMakeScale(1, -1)
myView.layer.borderWidth = 2
myView.addSubview(subView)
myView.backgroundColor = UIColor(red: 0, green: 0.8, blue: 0, alpha: 0.8)
Thanks #Mohit for your input in helping me to find the solution.

Resources