UIView confusion - ios

All , I am trying to figure out a UIView which gets bigger with this code :
ColouredSquare.backgroundColor = UIColor(red: 0.84, green: 0.90, blue: 0.95, alpha: 1.0)
ColouredSquare.frame = CGRect(x: 0, y:50, width: 400, height: 30)
self.view.addSubview(ColouredSquare)
UIView.animateWithDuration(1.0, animations: {
self.ColouredSquare.frame = CGRect(x: 0, y: 50, width: 400, height: 180)
})
I have added a button within the view.
But the button draws before the UIView.

Am I missing something obvious here?
You set the frame:
ColouredSquare.frame = CGRect(x: 0, y:50, width: 400, height: 30)
And then animate is, increasing the height to 180:
self.ColouredSquare.frame = CGRect(x: 0, y: 50, width: 400, height: 180)

Related

How to specify which child view does not inherit shadows when setting shadows for the parent view?

I want bView to be free of shadows. How can I achieve this?
I want to shadow all subviews of the superView, so I cannot remove the shadow settings of the superView.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
superView.layer.shadowColor = UIColor.lightGray.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
self.view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
I have tried bView.clipsToBounds = true but it's not working.
If you give superView a non-clear background color then none of its subviews will get a shadow. You can then apply a shadow to the subviews that you want to have a shadow.
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white // <-- Add this line
superView.layer.shadowColor = UIColor.black.cgColor
superView.layer.shadowOffset = CGSize(width: 0, height: 3)
superView.layer.shadowOpacity = 0.2
superView.layer.shadowRadius = 5
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
// Add shadow to aView
aView.layer.shadowColor = UIColor.black.cgColor
aView.layer.shadowOffset = CGSize(width: 0, height: 3)
aView.layer.shadowOpacity = 0.2
aView.layer.shadowRadius = 5
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)
If you have several views that need a shadow then I would define a method to make it easier:
extension UIView {
func addShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0, height: 3)
layer.shadowOpacity = 0.2
layer.shadowRadius = 5
}
}
Then your code becomes something like this:
let superView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 350))
superView.backgroundColor = .white
superView.addShadow()
view.addSubview(superView)
let aView: UIView = UIView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
aView.backgroundColor = .white
aView.addShadow()
superView.addSubview(aView)
let bView: UIView = UIView(frame: CGRect(x: 10, y: 230, width: 100, height: 100))
bView.backgroundColor = .white
superView.addSubview(bView)

Indent UITextField text and placeholder

I have programmatically created a UITextField in my UITableView footer like so:
let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
self.textArea = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
self.textArea.placeholder = "Add Item"
self.textArea.layer.borderColor = UIColor.gray.cgColor
self.textArea.layer.borderWidth = 1
customView.addSubview(self.textArea)
let button = UIButton(frame: CGRect(x: self.view.bounds.width - 50, y: 0, width: 50, height: 50))
button.setTitle("Add", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor(displayP3Red: 3.0 / 255.0, green: 0.0 / 255.0, blue: 113.0 / 255.0, alpha: 1.0)
button.addTarget(self, action: #selector(self.addWishListItem), for: .touchUpInside)
customView.addSubview(button)
self.tableView.tableFooterView = customView
My question is, how do I indent the UITextField text and placeholder text so it's not right at the edge of the left side?
I found this:
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: self.textArea.frame.height))
self.textArea.leftView = paddingView
self.textArea.leftViewMode = .always
Is there a better way on doing this?
You can create a customTextField and override the rects for text, placeholder, border.. basically everything.
import UIKit
class CustomTextField: UITextField {
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return CGRect.init(x: 10, y: -10, width: bounds.width, height: bounds.height)
}
}
You control the position of it by changing x and y values

Custom height of the navbar in iOS 11

I'm trying to set a custom height to my navbar. The code that i have works fine in iOS 11, Xcode 8; but now, i have Xcode 9 and iOS 11 and the code is not working. This is what i have...
var navBar: UINavigationBar = UINavigationBar()
override func viewDidLoad() {
super.viewDidLoad()
self.navBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height:82)
view.addSubview(navBar)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navBar.frame = CGRect(x: 0, y: 0, width:self.view.frame.width, height: 82)
}
Thanks for the help!!!
You have to set
self.navBar.frame = CGRect(x: 0, y: 0, width: (bounds?.width)!, height: (bounds?.height)! + height)
I found the solution!!!
I searched and i found an example in the Apple Documentation. I simply did this:
-I created a class of type UIView and i putted this:
class ExtendedNavBarView: UIView {
override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
layer.shadowOffset = CGSize(width: 0, height: CGFloat(1) / UIScreen.main.scale)
layer.shadowRadius = 0
layer.shadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor
layer.shadowOpacity = 0.25
}
}
-Then, i putted this in viewDidLoad:
navBar.shadowImage = #imageLiteral(resourceName: "TransparentPixel")
let extendedBar = ExtendedNavBarView()
extendedBar.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: 82)
view.addSubview(extendedBar)
extendedBar.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1.0)
self.navBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 82)
view.addSubview(navBar)

how to make cameraOverlayView along the y axis (swift3)

I am making a photo app that I want the imagePicker screen to have blocks of red to pre mask the photo before it crops. My following code gets a roadblock on the top x axis. I would like to place another red box along the entire y axis where the yellow rectangle is.
let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
blockView.backgroundColor = UIColor.red
imagePicker.cameraOverlayView = blockView
Please try the following :
let mainView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height-150))
let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
blockView.backgroundColor = UIColor.red
let blockView1 = UIView.init(frame: CGRect.init(x: 0, y: 170, width: 100, height: self.view.frame.size.height-320))
blockView1.backgroundColor = UIColor.green
mainView.addSubview(blockView)
mainView.addSubview(blockView1)
imagePicker.cameraOverlayView = mainView

Swift - If statement not working as it should

I'm trying to animate an UIButton once two textsfields have had text entered. The button animates even if there is no text entered. I thought maybe it was an issue with there being placeholder text, but that has no effect. I have tried != "" as well as != nil
Here's my code
override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
textFieldConfig()
loginButtonConfig("LOG IN")
}
func loginButtonConfig(title:String) {
let frameWidth = self.view.frame.width
let frameHeight = self.view.frame.height
var button = UIButton()
button.frame = CGRect(x: 0, y: 300, width: 0, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: 0, height: 50)
button.backgroundColor = UIColor(red: 24.0/255.0, green: 198.0/255.0, blue: 152.0/255.0, alpha: 1.0)
button.setTitle(title, forState: .Normal)
button.titleLabel?.textAlignment = NSTextAlignment.Center
self.view.addSubview(button)
//WHY ISN'T THE IF STATEMENT WORKING AS IT SHOULD???
if self.userTextField.text != nil && self.passwordTextField.text != nil {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
}
Any help would be appreciated. Thanks :)
Try this:
if !self.userTextField.text.isEmpty && !self.passwordTextField.isEmpty {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}
Try this:
if self.userTextField.text! != "" && self.passwordTextField.text! != "" {
UIView.animateWithDuration(1.0, animations: { () -> Void in
button.frame = CGRect(x: 0, y: 300, width: frameWidth, height: 50)
button.bounds = CGRect(x: 0, y: 0, width: frameWidth, height: 50)
})
}

Resources