ios- UIButton not work in the custom view(UIViewX) - ios

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
create a UIViewX in the View Controller or
add UIButton on custom view(UIViewX). And IBAction is print(" test ")
Everything work good.
But...
When add UIButton on custom view(UIViewX) and add animation code with UIViewX, button does't work if I press it
how to fix it that can response print(" test ")?
Thanks in advance.
Here is animation code :
var currentColorArrayIndex = -1
var colorArray:[(color1: UIColor, color2: UIColor)] = []
override func viewDidLoad() {
super.viewDidLoad()
colorArrayFunction()
backgroundAnimate()
}
colorArrayFunction & backgroundAnimate
Here is UIViewX code :
import UIKit
#IBDesignable
class UIViewXXXXX: UIView {
// MARK: - Gradient
#IBInspectable var firstColor: UIColor = UIColor.white {
didSet {
updateView()}}
#IBInspectable var secondColor: UIColor = UIColor.white {
didSet {
updateView()}}
#IBInspectable var horizontalGradient: Bool = false {
didSet {
updateView()}}
override class var layerClass: AnyClass {
get {
return CAGradientLayer.self}}
func updateView() {
let layer = self.layer as! CAGradientLayer
layer.colors = [ firstColor.cgColor, secondColor.cgColor ]
if (horizontalGradient) {
layer.startPoint = CGPoint(x: 0.0, y: 0.5)
layer.endPoint = CGPoint(x: 1.0, y: 0.5)
} else {
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 0, y: 1)
}}
// MARK: - Border
#IBInspectable public var borderColor: UIColor = UIColor.clear {
didSet {
layer.borderColor = borderColor.cgColor}}
#IBInspectable public var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth}}
#IBInspectable public var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
// MARK: - Shadow
#IBInspectable public var shadowOpacity: CGFloat = 0 {
didSet {
layer.shadowOpacity = Float(shadowOpacity)}}
#IBInspectable public var shadowColor: UIColor = UIColor.clear {
didSet {
layer.shadowColor = shadowColor.cgColor}}
#IBInspectable public var shadowRadius: CGFloat = 0 {
didSet {
layer.shadowRadius = shadowRadius}}
#IBInspectable public var shadowOffsetY: CGFloat = 0 {
didSet {
layer.shadowOffset.height = shadowOffsetY}}}

Related

how to set gradient layer width to be the same as button width?

I am new in iOS Development, I want to make gradient rounded button by using this designable class
#IBDesignable
class GradientButton: UIButton {
let gradientLayer = CAGradientLayer()
#IBInspectable
var topGradientColor: UIColor? {
didSet {
setGradient(topGradientColor: topGradientColor, bottomGradientColor: bottomGradientColor)
}
}
#IBInspectable
var bottomGradientColor: UIColor? {
didSet {
setGradient(topGradientColor: topGradientColor, bottomGradientColor: bottomGradientColor)
}
}
override func layoutSubviews() {
super.layoutSubviews()
updateCornerRadius()
}
#IBInspectable var fullRounded: Bool = false {
didSet {
updateCornerRadius()
}
}
#IBInspectable var cornerRadiusOfButton : CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadiusOfButton
}
}
func updateCornerRadius() {
layer.cornerRadius = fullRounded ? (frame.size.height / 2) : cornerRadiusOfButton
}
#IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
#IBInspectable var borderColor: UIColor? {
set {
guard let uiColor = newValue else { return }
layer.borderColor = uiColor.cgColor
}
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
}
}
private func setGradient(topGradientColor: UIColor?, bottomGradientColor: UIColor?) {
if let topGradientColor = topGradientColor, let bottomGradientColor = bottomGradientColor {
gradientLayer.frame = bounds
gradientLayer.colors = [topGradientColor.cgColor, bottomGradientColor.cgColor]
gradientLayer.borderColor = layer.borderColor
gradientLayer.borderWidth = layer.borderWidth
gradientLayer.cornerRadius = layer.cornerRadius
layer.insertSublayer(gradientLayer, at: 0)
} else {
gradientLayer.removeFromSuperlayer()
}
}
}
but here is the result:
as you can see the width of the gradient layer button is incorret.
here is the constraint layout I use:
I want that gradient layer to have the same width as the original button, I suspect that the problem is in this line
gradientLayer.frame = bounds
but unfortunately I don't know how to set the gradient layer width to be the same size as the original button programmatically in #IBDesignable class
could you please help me to solve this issue ? thanks in advance
Override layoutSubviews inside GradientButton and update the gradientLayer frame as,
override func layoutSubviews() {
super.layoutSubviews()
self.gradientLayer.frame = bounds
}

Make circular photos downloaded from google (google places, swift)

I'm working with google places and i have a VC with a tableView where i downloaded nearby places from the user position and in each cell of the tableView i add the information of the place and the photo of it. I created a custom class to make the photo circular
import UIKit
#IBDesignable
class RoundImage: UIImageView {
#IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
#IBInspectable var borderWidth: CGFloat = 0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
#IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
}
but the problem is that i download the photos of the places with this class
import UIKit
private let widthKey = "width"
private let heightKey = "height"
private let photoReferenceKey = "photo_reference"
class QPhoto: NSObject {
var width: Int?
var height: Int?
var photoRef: String?
init(photoInfo: [String:Any]) {
height = photoInfo[heightKey] as? Int
width = photoInfo[widthKey] as? Int
photoRef = photoInfo[photoReferenceKey] as? String
}
func getPhotoURL(maxWidth:Int) -> URL? {
if let ref = self.photoRef {
return NearbyPlaces.googlePhotoURL(photoReference: ref, maxWidth: maxWidth)
}
return nil
}
}
and i would like to know how can i adjust it, because with this class the photos that i download even though i put the RoundImage class at the imageview in the storyboard are always square
You need to set clipsToBounds to true. You can do that when you set the cornerRadius:
#IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.clipsToBounds = true
}
}

searchcontroller, roundedcorners not working

This view's order is like this
-View Controller
-Fake shadow view
-Container view
-Search Bar
In normal state, roundedcorner works correctly, but when I click on it, there is some shadow. When I type something on it just the view becomes rectengular
try this IBDesignable class and give rounded corner from story board
import Foundation
#IBDesignable class customView: UIView {
#IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
#IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
#IBInspectable var borderColor: UIColor = UIColor.gray {
didSet {
layer.borderColor = borderColor.cgColor
}
}
#IBInspectable var shadowColor: UIColor = UIColor.gray {
didSet {
layer.shadowColor = shadowColor.cgColor
}
}
#IBInspectable var shadowOpacity: Float = 1.0 {
didSet {
layer.shadowOpacity = shadowOpacity
}
}
#IBInspectable var shadowRadius: CGFloat = 1.0 {
didSet {
layer.shadowRadius = shadowRadius
}
}
#IBInspectable var masksToBounds: Bool = true {
didSet {
layer.masksToBounds = masksToBounds
}
}
#IBInspectable var shadowOffset: CGSize = CGSize(width: 12, height: 12) {
didSet {
layer.shadowOffset = shadowOffset
}
}
}

TextField Corner Radius : Not Sharp Corners

I am trying to add corner radius on uitextfield effect applies but not sharp corner.
Please check attached pictures.
I am expecting result like this.
Here is my Code for gray textfield.
self.layer.cornerRadius = 7.5
self.layer.borderWidth = 1.5
self.layer.borderColor = UIColor(red: 209/255.0, green: 209/255.0, blue: 209/255.0, alpha: 1.0).CGColor
self.backgroundColor = UIColor.whiteColor()
Here is my Code for Red textfield.
self.layer.cornerRadius = 7.5
self.layer.borderWidth = 1.5
self.layer.borderColor = TextFieldRedBorderColor.CGColor
self.backgroundColor = TextFieldRedBackgroundColor
There is only color difference between both code but still it's not working.
self.layer.masksToBounds = true
This will solves your issue
#IBOutlet var passwordboarderview: UIView!
#IBOutlet var textusername: UITextField!
#IBOutlet var txtpassword: UITextField!
passwordboarderview.layer.cornerRadius = 7.5
passwordboarderview.layer.borderColor = UIColor.whiteColor().CGColor
passwordboarderview.layer.borderWidth = 2.0
passwordboarderview.clipsToBounds = true
txtpassword.attributedPlaceholder = NSAttributedString(string:"Password",attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()])
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
textusername.leftView = paddingView
textusername.leftViewMode = .Always
textusername.layer.borderWidth = 2.0
textusername.layer.cornerRadius = 7.5
textusername.layer.borderColor = UIColor.grayColor().CGColor
textusername.clipsToBounds = true
Output :
Storyboard:
//
// FDTextField.swift
// FilloutDocs
//
// Created by Janak Thakkar on 15/03/16.
// Copyright © 2016 zetrixweb. All rights reserved.
//
import UIKit
private var maxLengthDictionary = [UITextField : Int]()
#IBDesignable
class ZWTextField : UITextField {
var topBorder: UIView?
var bottomBorder: UIView?
var leftBorder: UIView?
var rightBorder: UIView?
var leftimageview : UIImageView?
var rightimageview : UIImageView?
#IBInspectable var borderColor: UIColor = UIColor.clearColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}
#IBInspectable var ULText: Bool = false {
didSet {
let textRange = NSMakeRange(0, (self.text?.characters.count)!)
let attributedText = NSMutableAttributedString(string: (self.text)!)
attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
#IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
#IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
#IBInspectable var fitToWidth: Bool = false {
didSet {
adjustsFontSizeToFitWidth = fitToWidth
}
}
#IBInspectable var cursorColor : UIColor = UIColor.blueColor(){
didSet {
tintColor = cursorColor
}
}
#IBInspectable var placeHolderColor : UIColor = UIColor.lightGrayColor(){
didSet {
setValue(placeHolderColor, forKeyPath: "_placeholderLabel.textColor")
}
}
override func layoutSubviews() {
super.layoutSubviews()
}
#IBInspectable var rightImage : UIImage? {
didSet {
if rightImage != nil {
let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth : rightImage!.size.width + 10
rightViewMode = UITextFieldViewMode.Always
rightimageview = UIImageView()
rightimageview!.frame=CGRectMake(self.frame.size.width - width, self.frame.origin.y+2, width,self.frame.size.height-4)
rightimageview!.image = rightImage
rightView = rightimageview
self.rightViewMode = .Always
rightimageview!.contentMode = .Center
}
else {
if rightimageview != nil {
rightimageview?.removeFromSuperview()
rightimageview = nil
}
}
}
}
#IBInspectable var rightviewWidth : CGFloat = 0 {
didSet{
if rightimageview != nil{
let width = rightviewWidth > rightImage!.size.width + 10 ? rightviewWidth : rightImage!.size.width + 10
rightimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4)
}
}
}
#IBInspectable var leftImage : UIImage? {
didSet {
if leftImage != nil {
let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth : leftImage!.size.width + 10
leftViewMode = UITextFieldViewMode.Always
leftimageview = UIImageView();
leftimageview!.frame=CGRectMake(self.frame.origin.x+50, self.frame.origin.y+5, width,self.frame.size.height-5)
//leftimageview!.frame = CGRectMake(10, 5, 40, 40)
leftimageview!.image = leftImage;
leftView = leftimageview;
self.leftViewMode = .Always
leftimageview!.contentMode = .Center
}
}
}
#IBInspectable var leftviewWidth : CGFloat = 0 {
didSet{
if leftimageview != nil{
let width = leftviewWidth > leftImage!.size.width + 10 ? leftviewWidth : leftImage!.size.width + 10
leftimageview!.frame=CGRectMake(self.frame.origin.x+5, self.frame.origin.y+2, width,self.frame.size.height-4)
}
}
}
#IBInspectable var bottomLineWidth : CGFloat = 1 {
didSet{
let border: CALayer = CALayer()
border.borderColor = UIColor.darkGrayColor().CGColor
self.frame = CGRectMake(0, self.frame.size.height - bottomLineWidth, self.frame.size.width, self.frame.size.height)
border.borderWidth = borderWidth
self.layer.addSublayer(border)
self.layer.masksToBounds = true
}
}
#IBInspectable var bottomLineColor : UIColor = UIColor.lightGrayColor(){
didSet {
let border: CALayer = CALayer()
border.borderColor = bottomLineColor.CGColor
}
}
#IBInspectable var paddingLeft: CGFloat = 0
#IBInspectable var paddingRight: CGFloat = 0
override func textRectForBounds(bounds: CGRect) -> CGRect {
return CGRectMake(bounds.origin.x + paddingLeft, bounds.origin.y,
bounds.size.width - paddingLeft - paddingRight, bounds.size.height);
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
return textRectForBounds(bounds)
}
#IBInspectable var topBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var topBorderHeight : CGFloat = 0 {
didSet{
if topBorder == nil{
topBorder = UIView()
topBorder?.backgroundColor=topBorderColor;
topBorder?.frame = CGRectMake(0, 0, self.frame.size.width, topBorderHeight)
addSubview(topBorder!)
}
}
}
#IBInspectable var bottomBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var bottomBorderHeight : CGFloat = 0 {
didSet{
if bottomBorder == nil{
bottomBorder = UIView()
bottomBorder?.backgroundColor=bottomBorderColor;
bottomBorder?.frame = CGRectMake(0, self.frame.size.height - bottomBorderHeight, self.frame.size.width, bottomBorderHeight)
addSubview(bottomBorder!)
}
}
}
#IBInspectable var leftBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var leftBorderHeight : CGFloat = 0 {
didSet{
if leftBorder == nil{
leftBorder = UIView()
leftBorder?.backgroundColor=leftBorderColor;
leftBorder?.frame = CGRectMake(0, 0, leftBorderHeight, self.frame.size.height)
addSubview(leftBorder!)
}
}
}
#IBInspectable var rightBorderColor : UIColor = UIColor.clearColor()
#IBInspectable var rightBorderHeight : CGFloat = 0 {
didSet{
if rightBorder == nil{
rightBorder = UIView()
rightBorder?.backgroundColor=topBorderColor;
rightBorder?.frame = CGRectMake(self.frame.size.width - rightBorderHeight, 0, rightBorderHeight, self.frame.size.height)
addSubview(rightBorder!)
}
}
}
#IBInspectable var maxLength: Int {
get {
if let length = maxLengthDictionary[self] {
return length
} else {
return Int.max
}
}
set {
maxLengthDictionary[self] = newValue
addTarget(self, action: "checkMaxLength:", forControlEvents: UIControlEvents.EditingChanged)
}
}
func checkMaxLength(sender: UITextField) {
let newText = sender.text
if newText?.characters.count > maxLength {
let cursorPosition = selectedTextRange
text = (newText! as NSString).substringWithRange(NSRange(location: 0, length: maxLength))
selectedTextRange = cursorPosition
}
}
}
Just add this file in project and you can set all the properties and can see realtime out.All properties you can set through storyboard.
There was just one mistake in my code.
TextField Border Style Was Line
textField.borderStyle = .Line
then I make it None
textField.borderStyle = .None
and it work like I wanted (Rounded Corners)...
Anyway.. Thanks for support and answers...

'CGPointMake' is unavailable in swift [duplicate]

This question already has answers here:
CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift
(11 answers)
Closed 6 years ago.
I have a Gradient class which I am trying to convert to Swift 3 but I get the following error
'CGPointMake' is unavailable in swift
for
func configureGradientView() {
let color1 = topColor ?? self.tintColor as UIColor
let color2 = bottomColor ?? UIColor.black as UIColor
let colors: Array <AnyObject> = [ color1.cgColor, color2.cgColor ]
let layer = self.layer as! CAGradientLayer
layer.colors = colors
layer.startPoint = CGPointMake(startX, startY)
layer.endPoint = CGPointMake(endX, endY)
}
Can anyone help me out with what I can use instead of CGPointMake
Here's the full class;
#IBDesignable public class XGradientView: UIView {
#IBInspectable public var topColor: UIColor? {
didSet {
configureGradientView()
}
}
#IBInspectable public var bottomColor: UIColor? {
didSet {
configureGradientView()
}
}
#IBInspectable var startX: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
#IBInspectable var startY: CGFloat = 1.0 {
didSet{
configureGradientView()
}
}
#IBInspectable var endX: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
#IBInspectable var endY: CGFloat = 0.0 {
didSet{
configureGradientView()
}
}
public class func layeredClass() -> AnyClass {
return CAGradientLayer.self
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
configureGradientView()
}
override init(frame: CGRect) {
super.init(frame: frame)
configureGradientView()
}
public override func tintColorDidChange() {
super.tintColorDidChange()
configureGradientView()
}
func configureGradientView() {
let color1 = topColor ?? self.tintColor as UIColor
let color2 = bottomColor ?? UIColor.black as UIColor
let colors: Array <AnyObject> = [ color1.cgColor, color2.cgColor ]
let layer = self.layer as! CAGradientLayer
layer.colors = colors
layer.startPoint = CGPointMake(startX, startY)
layer.endPoint = CGPointMake(endX, endY)
}
}
In swift you can create a CGPoint in swifty way CGPoint(x: xPos, y:yPos).
So change your CGPointMake(startX, startY) to CGPoint(x: startX, y: startY)
swift 3 emphasizes in use of named Parameters.
CGPoint Can be created like this.
let point = CGPoint(x: 0,y :0) // CGFloat, Double, Int

Resources