Skip to content

Commit b74181f

Browse files
committed
Support for GlassEffect
1 parent f0b00e2 commit b74181f

2 files changed

Lines changed: 60 additions & 29 deletions

File tree

Example/Sources/InputBar Examples/GlassInputBar.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,34 @@ final class GlassInputBar: InputBarAccessoryView {
2424
backgroundColor = nil
2525
separatorLine.isHidden = true
2626
if #available(iOS 26.0, *) {
27-
backgroundView.effect = UIGlassContainerEffect()
27+
let container = UIGlassContainerEffect()
28+
container.spacing = 8
29+
backgroundView.effect = container
2830
backgroundView.backgroundColor = nil
2931

32+
middleContentViewPadding.left = 8
33+
middleContentViewPadding.right = 8
34+
3035
let effect = UIGlassEffect(style: .regular)
3136
effect.isInteractive = true
32-
contentView.cornerConfiguration = .capsule()
33-
contentView.effect = effect
37+
middleContentViewWrapper.cornerConfiguration = .capsule()
38+
middleContentViewWrapper.effect = effect
39+
40+
leftContentView.cornerConfiguration = .capsule()
41+
leftContentView.effect = effect
42+
43+
rightContentView.cornerConfiguration = .capsule()
44+
rightContentView.effect = effect
3445
}
3546

36-
47+
let button = InputBarButtonItem()
48+
button.setSize(CGSize(width: 36, height: 36), animated: false)
49+
button.setImage(#imageLiteral(resourceName: "ic_plus").withRenderingMode(.alwaysTemplate), for: .normal)
50+
button.imageView?.contentMode = .scaleAspectFit
51+
button.tintColor = .systemBlue
52+
53+
setLeftStackViewWidthConstant(to: 36, animated: false)
54+
setStackViewItems([button], forStack: .left, animated: false)
3755
}
3856
}
3957

Sources/InputBarAccessoryView.swift

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ open class InputBarAccessoryView: UIView {
7676
1. It's axis is initially set to .horizontal
7777
*/
7878
public let leftStackView = InputStackView(axis: .horizontal, spacing: 0)
79+
public let leftContentView: UIVisualEffectView = {
80+
let view = UIVisualEffectView()
81+
view.translatesAutoresizingMaskIntoConstraints = false
82+
return view
83+
}()
7984

8085
/**
8186
The InputStackView at the InputStackView.right position
@@ -84,6 +89,11 @@ open class InputBarAccessoryView: UIView {
8489
1. It's axis is initially set to .horizontal
8590
*/
8691
public let rightStackView = InputStackView(axis: .horizontal, spacing: 0)
92+
public let rightContentView: UIVisualEffectView = {
93+
let view = UIVisualEffectView()
94+
view.translatesAutoresizingMaskIntoConstraints = false
95+
return view
96+
}()
8797

8898
/**
8999
The InputStackView at the InputStackView.bottom position
@@ -107,8 +117,8 @@ open class InputBarAccessoryView: UIView {
107117
public private(set) weak var middleContentView: UIView?
108118

109119
/// A view to wrap the `middleContentView` inside
110-
private let middleContentViewWrapper: UIView = {
111-
let view = UIView()
120+
public let middleContentViewWrapper: UIVisualEffectView = {
121+
let view = UIVisualEffectView()
112122
view.translatesAutoresizingMaskIntoConstraints = false
113123
return view
114124
}()
@@ -403,13 +413,15 @@ open class InputBarAccessoryView: UIView {
403413

404414
addSubview(backgroundView)
405415
addSubview(topStackView)
406-
addSubview(contentView)
416+
backgroundView.contentView.addSubview(contentView)
407417
addSubview(separatorLine)
408-
contentView.addSubview(middleContentViewWrapper)
409-
contentView.addSubview(leftStackView)
410-
contentView.addSubview(rightStackView)
411-
contentView.addSubview(bottomStackView)
412-
middleContentViewWrapper.addSubview(inputTextView)
418+
contentView.contentView.addSubview(middleContentViewWrapper)
419+
contentView.contentView.addSubview(leftContentView)
420+
leftContentView.contentView.addSubview(leftStackView)
421+
contentView.contentView.addSubview(rightContentView)
422+
rightContentView.contentView.addSubview(rightStackView)
423+
contentView.contentView.addSubview(bottomStackView)
424+
middleContentViewWrapper.contentView.addSubview(inputTextView)
413425
middleContentView = inputTextView
414426
setStackViewItems([sendButton], forStack: .right, animated: false)
415427
}
@@ -445,26 +457,27 @@ open class InputBarAccessoryView: UIView {
445457
middleContentViewLayoutSet = NSLayoutConstraintSet(
446458
top: middleContentViewWrapper.topAnchor.constraint(equalTo: contentView.topAnchor, constant: middleContentViewPadding.top),
447459
bottom: middleContentViewWrapper.bottomAnchor.constraint(equalTo: bottomStackView.topAnchor, constant: -middleContentViewPadding.bottom),
448-
left: middleContentViewWrapper.leftAnchor.constraint(equalTo: leftStackView.rightAnchor, constant: middleContentViewPadding.left),
449-
right: middleContentViewWrapper.rightAnchor.constraint(equalTo: rightStackView.leftAnchor, constant: -middleContentViewPadding.right)
460+
left: middleContentViewWrapper.leftAnchor.constraint(equalTo: leftContentView.rightAnchor, constant: middleContentViewPadding.left),
461+
right: middleContentViewWrapper.rightAnchor.constraint(equalTo: rightContentView.leftAnchor, constant: -middleContentViewPadding.right)
450462
)
451463

452464
inputTextView.fillSuperview()
453465
maxTextViewHeight = calculateMaxTextViewHeight()
454466
textViewHeightAnchor = inputTextView.heightAnchor.constraint(equalToConstant: maxTextViewHeight)
455467

456468
leftStackViewLayoutSet = NSLayoutConstraintSet(
457-
top: leftStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0),
458-
bottom: leftStackView.bottomAnchor.constraint(equalTo: middleContentViewWrapper.bottomAnchor, constant: 0),
459-
left: leftStackView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0),
460-
width: leftStackView.widthAnchor.constraint(equalToConstant: leftStackViewWidthConstant)
469+
top: leftContentView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0),
470+
bottom: leftContentView.bottomAnchor.constraint(equalTo: middleContentViewWrapper.bottomAnchor, constant: 0),
471+
left: leftContentView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0),
472+
width: leftContentView.widthAnchor.constraint(equalToConstant: leftStackViewWidthConstant)
461473
)
462474

475+
rightStackView.fillSuperview()
463476
rightStackViewLayoutSet = NSLayoutConstraintSet(
464-
top: rightStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0),
465-
bottom: rightStackView.bottomAnchor.constraint(equalTo: middleContentViewWrapper.bottomAnchor, constant: 0),
466-
right: rightStackView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0),
467-
width: rightStackView.widthAnchor.constraint(equalToConstant: rightStackViewWidthConstant)
477+
top: rightContentView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0),
478+
bottom: rightContentView.bottomAnchor.constraint(equalTo: middleContentViewWrapper.bottomAnchor, constant: 0),
479+
right: rightContentView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0),
480+
width: rightContentView.widthAnchor.constraint(equalToConstant: rightStackViewWidthConstant)
468481
)
469482

470483
bottomStackViewLayoutSet = NSLayoutConstraintSet(
@@ -604,11 +617,11 @@ open class InputBarAccessoryView: UIView {
604617
for position in positions {
605618
switch position {
606619
case .left:
607-
leftStackView.setNeedsLayout()
608-
leftStackView.layoutIfNeeded()
620+
leftContentView.setNeedsLayout()
621+
leftContentView.layoutIfNeeded()
609622
case .right:
610-
rightStackView.setNeedsLayout()
611-
rightStackView.layoutIfNeeded()
623+
rightContentView.setNeedsLayout()
624+
rightContentView.layoutIfNeeded()
612625
case .bottom:
613626
bottomStackView.setNeedsLayout()
614627
bottomStackView.layoutIfNeeded()
@@ -671,7 +684,7 @@ open class InputBarAccessoryView: UIView {
671684
middleContentView?.removeFromSuperview()
672685
middleContentView = view
673686
guard let view = view else { return }
674-
middleContentViewWrapper.addSubview(view)
687+
middleContentViewWrapper.contentView.addSubview(view)
675688
view.fillSuperview()
676689

677690
performLayout(animated) { [weak self] in
@@ -710,7 +723,7 @@ open class InputBarAccessoryView: UIView {
710723
}
711724
}
712725
guard superview != nil else { return }
713-
leftStackView.layoutIfNeeded()
726+
leftContentView.layoutIfNeeded()
714727
case .right:
715728
rightStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
716729
rightStackViewItems = items
@@ -722,7 +735,7 @@ open class InputBarAccessoryView: UIView {
722735
}
723736
}
724737
guard superview != nil else { return }
725-
rightStackView.layoutIfNeeded()
738+
rightContentView.layoutIfNeeded()
726739
case .bottom:
727740
bottomStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
728741
bottomStackViewItems = items

0 commit comments

Comments
 (0)