How do I get button shadow working with Swift 4.2? - ios

I am trying to get my button to render a shadow in swift 4.2
I've followed other examples to no avail. What am I missing?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var btnHi: UIButton! // Standard button tweeked in view controller
#IBOutlet weak var btnNext: NextButton! // My custom button via class
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
btnHi.layer.cornerRadius = btnHi.frame.height / 2
btnHi.layer.borderWidth = 3.0
btnHi.layer.borderColor = UIColor.darkGray.cgColor
btnHi.layer.shadowColor = UIColor.black.cgColor
btnHi.layer.shadowOffset = CGSize(width: 0.0, height: 6.0)
btnHi.layer.shadowRadius = 8
btnHi.layer.opacity = 0.5
// btnHi.clipsToBounds = true
// btnHi.layer.masksToBounds = false
}
#IBAction func btnNext(_ sender: Any) {
btnNext.setupShakeButton()
}
}
should see a drop shadow for the button instead I get no shadow.

You are missing shadowOpacity which is zero by default. You should probably not use opacity because that makes the whole button semitransparent.
btnHi.layer.shadowColor = UIColor.black.cgColor
btnHi.layer.shadowOffset = CGSize(width: 0.0, height: 6.0)
btnHi.layer.shadowRadius = 8
btnHi.layer.shadowOpacity = 0.5
Also note that clipping has to be turned off:
btnHi.layer.masksToBounds = false

Related

UIView rounded border without bottom or top side [duplicate]

This question already has answers here:
Rounded Corners only on Top of a UIView
(11 answers)
Closed 1 year ago.
I have UIView which I need to draw a border only partial part view, this is how my design look alike, how do i make it ?
expected
Try this
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var droppedView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
droppedView.clipsToBounds = true
droppedView.layer.cornerRadius = 24 // 얼만큼 둥글게 할 것인지
droppedView.layer.maskedCorners = [.layerMinXMinYCorner,
.layerMaxXMinYCorner]
droppedView.backgroundColor = .systemGray6
}
}
#IBOutlet weak var shadowVw: UIView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
shadowVw.layer.cornerRadius = 24
shadowVw.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
shadowVw.backgroundColor = .systemGray6
shadowVw.layer.shadowColor = UIColor.lightGray.cgColor
shadowVw.layer.shadowOpacity = 0.7
shadowVw.layer.shadowOffset = CGSize(width: 0.0, height: -5.0)
shadowVw.layer.shadowRadius = 5
}

Clip button to view

enter image description hereSo i have backgroundView (UIView) and two buttons in it.
Now i have made following code
buttonBackgroundView.layer.cornerRadius = 5.0
buttonBackgroundView.layer.borderColor = UIColor.black.cgColor
buttonBackgroundView.layer.borderWidth = 0.5
#IBOutlet weak var firstbutton: UIButton!{
didSet{
proceedButton.clipsToBounds = true
}
}
in this the view has rounded corner but the button does not have those rounded corners
How do i implement that? (so that button also get the rounded corner.
I have two buttons, so I want only the left corner of left button and right corner of right button.
I have added the image link which i want to achieve.
class ViewController: UIViewController {
#IBOutlet weak var yourView: UIView! //Create your view outlet
override func viewDidLoad() {
super.viewDidLoad()
yourView.clipsToBounds = true
yourView.layer.cornerRadius = 15 // As per you requirement
yourView.layer.borderColor = UIColor.black.cgColor
yourView.layer.borderWidth = 2
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Add Following constraint
Output:
This might help you :)
Please try using masksToBounds property,
#IBOutlet weak var firstbutton: UIButton!{
didSet{
proceedButton.clipsToBounds = true
proceedButton.masksToBounds = true
}
}

Xcode how to combine UI buttons?

i'm making a custom keyboard and wanna add shadow to buttons.
so added some code(keys.layer~~) in viewDidLoad()
but i can't set multiple button to one IBOutlet
what i want is all keys have 'keys(IBOutlet)' as value(?)
is it possible? if it's not, any other thing is fine :)
help!
this is my code (pls ignore next keyboard button)
#IBAction func keys(sender: AnyObject) {
}
#IBOutlet weak var keys: UIButton!
#IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
loadInterface()
keys.layer.cornerRadius = 4.0;
keys.layer.shadowColor = UIColor.blackColor().CGColor
keys.layer.shadowOffset = CGSizeMake(0.0, 2.0)
keys.layer.masksToBounds = false
keys.layer.shadowRadius = 1.0
keys.layer.shadowOpacity = 0.5
You can make UIButton extension like this and used with all the object of your custom keyboard Button
extension UIButton {
func setShadow() {
self.layer.cornerRadius = 4.0;
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSizeMake(0.0, 2.0)
self.layer.masksToBounds = false
self.layer.shadowRadius = 1.0
self.layer.shadowOpacity = 0.5
}
}
Now you can call this method with your button object like this way
self.nextKeyboardButton.setShadow()
Edit:
If you want to access multiple Button object with same object you can create an Array of UIButton, and store all the Outlet Button inside that array.
var buttonArray = [UIButton]()

Positioning images and that are unhidden

I'm working on a project to have hidden images appear when the button is pressed. My only problem is that when I click on the button the image appears. I then have to click on the button again for it to show up in the correct area of my view controller. Can someone help point me in the right direction please?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.hidden = true
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.hidden = false
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
}
This may or may not help, but i would recommend that instead of using the image.hidden property, set the alpha value of the image. So image.alpha = 0 for hidden and image.alpha = 1 for visible. This would also allow you to then fade the image in and out if you wanted. Additionally I would set the image in viewDidLoad() instead of the Action. Here's how I would redo your code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.alpha = 0
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.alpha = 1
}
}
I also believe setting the frame twice isn't useful as the frame will be set in viewDidLoad()

How to access the value of a UIStepper inside a Cocoa Touch Class

I'm trying to create a custom slider, using a cocoa touch class, whose maximum value is determined by a UIStepper. I have the UIStepper wired to my view controller and I want to reference its value inside the cocoa touch class as the slider's maximum value. What is the syntax for referencing the stepper's value inside the class?
I keep getting the error use of unresolved identifier.
Here is my viewController:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var stepperValue: UIStepper!
#IBOutlet weak var label: UILabel!
let slider1 = Slider1(frame: CGRectZero)
override func viewDidLoad() {
super.viewDidLoad()
label.textColor = UIColor.darkTextColor()
slider1.backgroundColor = UIColor.lightGrayColor()
view.addSubview(slider1)
}
override func viewDidLayoutSubviews() {
let margin: CGFloat = 20.0
let width = view.bounds.width - 2 * margin
slider1.frame = CGRect(x: margin, y: 3 * margin, width: width, height: 1.5 * margin)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Here is my UIControl subclass:
class Slider1: UIControl {
var minimumValue = 0.0
var maximumValue = stepperValue.value
var value = 0
let trackLayer = CALayer()
var trackHeight:CGFloat = 2.0
var trackColor = UIColor.blackColor().CGColor
var tickHeight:CGFloat = 8.0
var tickWidth: CGFloat = 2.0
var tickColor = UIColor.blackColor().CGColor
let thumbLayer = CALayer()
var thumbColor = UIColor.blackColor().CGColor
var thumbMargin:CGFloat = 2.0
var thumbWidth: CGFloat {
return CGFloat(bounds.height)
}
}
If you want a slider, use a slider. You will have a LOT of work to do if you want to create your own slider-like control starting from UIControl.
Just make your view controller drive everything.
Have the value changed event on your stepper trigger an action in the view controller.
In that stepperValueChanged action, fetch the new value of the stepper and use it to change the maximum value of the slider. That's all there is to it.

Resources