Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 42 additions & 2 deletions Sources/TSAlert/TSAlertController+ViewConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public extension TSAlertController {
public var shadow: Shadow?

/// The corner radius of the alert view.
public var cornerRadius: CGFloat
public var cornerRadius: CornerRadius

/// The background color of the dimmed overlay behind the alert.
public var dimmedBackgroundViewColor: Background?
Expand All @@ -95,6 +95,46 @@ public extension TSAlertController {
/// The axis layout of the button group (horizontal or vertical).
public var buttonGroupAxis: ButtonGroupAxis

///
public struct CornerRadius: ExpressibleByFloatLiteral {

///
public var topLeft: CGFloat

///
public var topRight: CGFloat

///
public var bottomLeft: CGFloat

///
public var bottomRight: CGFloat

///
public init(allCorners: CGFloat = 20) {
self.init(topLeft: allCorners,
topRight: allCorners,
bottomLeft: allCorners,
bottomRight: allCorners)
}

///
public init(topLeft: CGFloat = 20,
topRight: CGFloat = 20,
bottomLeft: CGFloat = 20,
bottomRight: CGFloat = 20) {
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
}

///
public init(floatLiteral value: FloatLiteralType) {
self.init(allCorners: value)
}
}

/// Defines the shadow properties of the alert.
public struct Shadow {

Expand Down Expand Up @@ -172,7 +212,7 @@ public extension TSAlertController {
backgroundBorderColor: CGColor? = nil,
backgroundBorderWidth: CGFloat = 0,
shadow: Shadow? = nil,
cornerRadius: CGFloat = 20,
cornerRadius: CornerRadius = .init(allCorners: 20),
dimmedBackgroundViewColor: Background? = .color(.black.withAlphaComponent(0.75)),
margin: LayoutMargin = .init(),
spacing: LayoutSpacing = .init(),
Expand Down
20 changes: 19 additions & 1 deletion Sources/TSAlert/TSAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ public final class TSAlertController: UIViewController {
activateFirstResponderIfNeeded()
}

public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

view.applyRoundCorners(viewConfiguration.cornerRadius)
}

public override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

Expand Down Expand Up @@ -303,6 +309,7 @@ public final class TSAlertController: UIViewController {
/// based on `TSAlertController.Style`.
/// This ensures that the alert is presented correctly and prevents unintended behavior.
private func adjustViewConfigurationForcibly() {
adjustCornerRadiusForcibly()
adjustButtonGroupAxisForcibly()
adjustActionOrderForcibly()
}
Expand Down Expand Up @@ -333,6 +340,17 @@ public final class TSAlertController: UIViewController {
}
}

///
private func adjustCornerRadiusForcibly() {
switch preferredStyle {
case .actionSheet:
viewConfiguration.cornerRadius.bottomLeft = 0
viewConfiguration.cornerRadius.bottomRight = 0
default:
break
}
}

private func initializeAlertView() {
setupAlertView()
setupConstraints()
Expand Down Expand Up @@ -384,7 +402,7 @@ public final class TSAlertController: UIViewController {

view.layer.borderColor = viewConfiguration.backgroundBorderColor
view.layer.borderWidth = viewConfiguration.backgroundBorderWidth
view.layer.cornerRadius = viewConfiguration.cornerRadius
view.applyRoundCorners(viewConfiguration.cornerRadius)

guard let shadow = viewConfiguration.shadow else { return }
view.layer.shadowColor = shadow.color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ class DefaultAlertView: UIView, TSAlertView {
let actionHeight: CGFloat = viewConfiguration.buttonHeight
let spacing: CGFloat = viewConfiguration.spacing.buttonSpacing

let height: CGFloat = if !isEmpty {
isHorizontal
var height: CGFloat = 0
if !isEmpty {
height = isHorizontal
? actionHeight
: (actionHeight * actionsCount) + ((actionsCount - 1) * spacing)
} else {
0
}
buttonGroupView.setHeight(equalTo: height)
}
Expand Down
59 changes: 59 additions & 0 deletions Sources/Utils/Extensions/UIBezierPath+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// UIBezierPath+Extension.swift
// TSAlertController
//
// Created by 김건우 on 3/21/25.
//

import UIKit

extension UIBezierPath {

convenience init(roundedRect rect: CGRect,
topLeftRadius: CGFloat = .zero,
topRightRadius: CGFloat = .zero,
bottomLeftRadius: CGFloat = .zero,
bottomRightRadius: CGFloat = .zero) {
self.init()

let path = CGMutablePath()

let topLeftPoint = CGPoint(x: rect.minX, y: rect.minY)
let topRightPoint = CGPoint(x: rect.maxX, y: rect.minY)
let bottomLeftPoint = CGPoint(x: rect.minX, y: rect.maxY)
let bottomRightPoint = CGPoint(x: rect.maxX, y: rect.maxY)

path.move(to: bottomLeftPoint)

path.addArc(tangent1End: topLeftPoint,
tangent2End: topRightPoint,
radius: topLeftRadius)

path.addArc(tangent1End: topRightPoint,
tangent2End: bottomRightPoint,
radius: topRightRadius)

path.addArc(tangent1End: bottomRightPoint,
tangent2End: bottomLeftPoint,
radius: bottomRightRadius)

path.addArc(tangent1End: bottomLeftPoint,
tangent2End: topLeftPoint,
radius: bottomLeftRadius)

path.closeSubpath()
cgPath = path
}
}


extension CGMutablePath {

func addLine(toX x: CGFloat, y: CGFloat) {
addLine(to: CGPoint(x: x, y: y))
}

func move(toX x: CGFloat, y: CGFloat) {
move(to: CGPoint(x: x, y: y))
}
}
48 changes: 45 additions & 3 deletions Sources/Utils/Extensions/UIView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ extension UIView {
if let configuration = configuration {
blurEffectView.layer.borderColor = configuration.backgroundBorderColor
blurEffectView.layer.borderWidth = configuration.backgroundBorderWidth
blurEffectView.layer.cornerRadius = configuration.cornerRadius
// blurEffectView.layer.cornerRadius = configuration.cornerRadius
blurEffectView.layer.masksToBounds = true
}
insertSubview(blurEffectView, at: 0)
Expand All @@ -242,14 +242,14 @@ extension UIView {
locations)

if let viewConfiguration = viewConfiguration {
gradientView.gradientLayer?.cornerRadius = viewConfiguration.cornerRadius
// gradientView.gradientLayer?.cornerRadius = viewConfiguration.cornerRadius
gradientView.gradientLayer?.masksToBounds = true
}
insertSubview(gradientView, at: 0)
gradientView.fill(to: self)
}

class GradientView: UIView {
final class GradientView: UIView {

let gradientLayer: CAGradientLayer?

Expand Down Expand Up @@ -310,3 +310,45 @@ extension UIView {
self.layer.anchorPoint = anchorPoint
}
}

extension UIView {

// MARK: - Apply Round Corners

///
func applyRoundCorners(_ cornerRadius: CGFloat) {

applyRoundCorners(topLeftRadius: cornerRadius,
topRightRadius: cornerRadius,
bottomLeftRadius: cornerRadius,
bottomRightRadius: cornerRadius)
}

///
func applyRoundCorners(_ cornerRadius: TSAlertController.ViewConfiguration.CornerRadius) {

applyRoundCorners(topLeftRadius: cornerRadius.topLeft,
topRightRadius: cornerRadius.topRight,
bottomLeftRadius: cornerRadius.bottomLeft,
bottomRightRadius: cornerRadius.bottomRight)
}

///
func applyRoundCorners(topLeftRadius: CGFloat,
topRightRadius: CGFloat,
bottomLeftRadius: CGFloat,
bottomRightRadius: CGFloat) {

let roundedRect = self.bounds

let path = UIBezierPath(roundedRect: roundedRect,
topLeftRadius: topLeftRadius,
topRightRadius: topRightRadius,
bottomLeftRadius: bottomLeftRadius,
bottomRightRadius: bottomRightRadius)

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
layer.mask = shapeLayer
}
}