Skip to content

Commit 25da4ad

Browse files
author
Bartłomiej Semańczyk
committed
Change layout to make it similar to native ios 10 notification.
1 parent 23c8bf6 commit 25da4ad

9 files changed

Lines changed: 222 additions & 164 deletions

File tree

Assets/1.png

191 KB
Loading

Assets/2.png

371 KB
Loading

Assets/3.png

309 KB
Loading

Assets/4.png

428 KB
Loading

BSForegroundNotification.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
Pod::Spec.new do |s|
1010
s.name = "BSForegroundNotification"
11-
s.version = "1.3"
12-
s.summary = "Present your custom iOS 8 and iOS 9 notification alert when app is in foreground mode."
13-
s.description = "If you need present notification that looks like a native notifaction in iOS 8 or 9 with custom actions including textfield while app is in foreground mode... this framework is for you:-) Simple and straightforward in use."
11+
s.version = "1.6"
12+
s.summary = "Present your custom iOS 10 notification alert when app is in foreground mode."
13+
s.description = "If you need present notification that looks like a native notifaction in iOS 10 with custom actions including textfield while app is in foreground mode... this framework is for you:-) Simple and straightforward in use."
1414

1515
s.homepage = "https://github.com/kunass2/BSForegroundNotification"
1616
s.license = 'MIT'

Example/BSForegroundNotification.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,12 @@
221221
TargetAttributes = {
222222
607FACCF1AFB9204008FA782 = {
223223
CreatedOnToolsVersion = 6.3.1;
224-
DevelopmentTeam = 78CQWFZKPC;
224+
DevelopmentTeam = J8AS95NKP2;
225225
LastSwiftMigration = 0800;
226226
};
227227
607FACE41AFB9204008FA782 = {
228228
CreatedOnToolsVersion = 6.3.1;
229+
DevelopmentTeam = J8AS95NKP2;
229230
LastSwiftMigration = 0800;
230231
TestTargetID = 607FACCF1AFB9204008FA782;
231232
};
@@ -436,6 +437,7 @@
436437
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: Bartłomiej Semańczyk (E4N889748T)";
437438
COPY_PHASE_STRIP = NO;
438439
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
440+
DEVELOPMENT_TEAM = J8AS95NKP2;
439441
ENABLE_STRICT_OBJC_MSGSEND = YES;
440442
ENABLE_TESTABILITY = YES;
441443
GCC_C_LANGUAGE_STANDARD = gnu99;
@@ -485,6 +487,7 @@
485487
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer: Bartłomiej Semańczyk (E4N889748T)";
486488
COPY_PHASE_STRIP = NO;
487489
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
490+
DEVELOPMENT_TEAM = J8AS95NKP2;
488491
ENABLE_NS_ASSERTIONS = NO;
489492
ENABLE_STRICT_OBJC_MSGSEND = YES;
490493
GCC_C_LANGUAGE_STANDARD = gnu99;
@@ -512,6 +515,7 @@
512515
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
513516
CODE_SIGN_IDENTITY = "iPhone Developer";
514517
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
518+
DEVELOPMENT_TEAM = J8AS95NKP2;
515519
INFOPLIST_FILE = BSForegroundNotification/Info.plist;
516520
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
517521
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -532,6 +536,7 @@
532536
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
533537
CODE_SIGN_IDENTITY = "iPhone Developer";
534538
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
539+
DEVELOPMENT_TEAM = J8AS95NKP2;
535540
INFOPLIST_FILE = BSForegroundNotification/Info.plist;
536541
IPHONEOS_DEPLOYMENT_TARGET = 8.3;
537542
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -550,6 +555,8 @@
550555
buildSettings = {
551556
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
552557
BUNDLE_LOADER = "$(TEST_HOST)";
558+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
559+
DEVELOPMENT_TEAM = J8AS95NKP2;
553560
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
554561
GCC_PREPROCESSOR_DEFINITIONS = (
555562
"DEBUG=1",
@@ -571,6 +578,7 @@
571578
buildSettings = {
572579
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
573580
BUNDLE_LOADER = "$(TEST_HOST)";
581+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
574582
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
575583
INFOPLIST_FILE = Tests/Info.plist;
576584
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";

Example/BSForegroundNotification/Images.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"size" : "29x29",
515
"idiom" : "iphone",
@@ -36,6 +46,16 @@
3646
"filename" : "Icon-60@3x.png",
3747
"scale" : "3x"
3848
},
49+
{
50+
"idiom" : "ipad",
51+
"size" : "20x20",
52+
"scale" : "1x"
53+
},
54+
{
55+
"idiom" : "ipad",
56+
"size" : "20x20",
57+
"scale" : "2x"
58+
},
3959
{
4060
"size" : "29x29",
4161
"idiom" : "ipad",

Pod/Classes/BSForegroundNotificationView.swift

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
2121
@IBOutlet private var heightTextContainerLayoutConstraint: NSLayoutConstraint!
2222
@IBOutlet private var heightDoubleButtonsLayoutConstraint: NSLayoutConstraint!
2323
@IBOutlet private var heightSingleButtonLayoutConstraint: NSLayoutConstraint!
24+
@IBOutlet private var heightPullViewLayoutConstraint: NSLayoutConstraint!
2425

2526
@IBOutlet private var appIconImageView: UIImageView!
26-
@IBOutlet private var separatorView: UIView!
2727

2828
@IBOutlet var titleLabel: UILabel!
2929
@IBOutlet var subtitleLabel: UILabel!
@@ -51,7 +51,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
5151

5252
private var currentHeightContainerLayoutConstraint: NSLayoutConstraint?
5353

54-
private var initialHeightForNotification: CGFloat = 80
54+
private var initialHeightForNotification: CGFloat = 100 //+20
5555
private var shouldShowTextView: Bool {
5656

5757
get {
@@ -68,10 +68,10 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
6868

6969
get {
7070

71-
var height = heightForText(subtitleLabel.text ?? "", width: subtitleLabel.frame.size.width) + 65
71+
var height = heightForText(subtitleLabel.text ?? "", width: subtitleLabel.frame.size.width) + 75
7272

7373
if let _ = currentHeightContainerLayoutConstraint {
74-
height += 50
74+
height += 60
7575
}
7676

7777
return height
@@ -130,10 +130,16 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
130130
super.awakeFromNib()
131131

132132
appIconImageView.image = UIImage(named: "AppIcon40x40")
133+
134+
NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: .UIDeviceOrientationDidChange, object: nil)
133135
}
134136

135137
//MARK: - Deinitialization
136138

139+
deinit {
140+
NotificationCenter.default.removeObserver(self)
141+
}
142+
137143
//MARK: - Actions
138144

139145
@IBAction func viewPanned(_ gestureRecognizer: UIPanGestureRecognizer) {
@@ -169,7 +175,8 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
169175
rightActionButton.alpha = alpha
170176
singleActionButton.alpha = alpha
171177
textView.alpha = alpha
172-
sendButton.alpha = alpha / 5
178+
sendButton.alpha = alpha
179+
173180
currentHeightContainerLayoutConstraint?.constant = 50 - (maxHeightOfNotification - frame.size.height)
174181
}
175182
}
@@ -314,7 +321,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
314321

315322
tapGestureRecognizer.isEnabled = false
316323
panGestureRecognizer.isEnabled = false
317-
sender.alpha = 0.2
324+
sender.alpha = 0.9
318325
}
319326

320327
@IBAction func actionButtonLeft(_ sender: UIButton) {
@@ -328,6 +335,21 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
328335

329336
//MARK: - Internal
330337

338+
func orientationDidChange() {
339+
340+
if extendingIsFinished {
341+
342+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1 as DispatchTime , execute: {
343+
344+
UIView.animate(withDuration: 0.3) {
345+
346+
self.heightConstraintNotification.constant = self.maxHeightOfNotification
347+
self.superview?.layoutIfNeeded()
348+
}
349+
})
350+
}
351+
}
352+
331353
func setupNotification() {
332354

333355
if let window = UIApplication.shared.keyWindow , !titleLabel.text!.isEmpty && !subtitleLabel.text!.isEmpty {
@@ -363,7 +385,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
363385
UIView.animate(withDuration: 0.5, animations: {
364386

365387
self.topConstraintNotification.constant = 0
366-
self.layoutIfNeeded()
388+
self.superview?.layoutIfNeeded()
367389
})
368390

369391
if let soundName = soundName {
@@ -391,7 +413,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
391413
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: .beginFromCurrentState, animations: {
392414

393415
self.topConstraintNotification.constant = -self.heightConstraintNotification.constant
394-
self.layoutIfNeeded()
416+
self.superview?.layoutIfNeeded()
395417

396418
}, completion: { finished in
397419

@@ -411,13 +433,12 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
411433

412434
//MARK: - Private
413435

414-
fileprivate func setupActions() {
436+
private func setupActions() {
415437

416438
heightTextContainerLayoutConstraint.constant = 0
417439
heightSingleButtonLayoutConstraint.constant = 0
418440
heightDoubleButtonsLayoutConstraint.constant = 0
419441

420-
separatorView.alpha = 0
421442
leftActionButton.alpha = 0
422443
rightActionButton.alpha = 0
423444
singleActionButton.alpha = 0
@@ -426,7 +447,6 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
426447

427448
if let actions = UIApplication.shared.currentUserNotificationSettings?.categories?.filter({ return $0.identifier == categoryIdentifier }).first?.actions(for: .default) , actions.count > 0 {
428449

429-
separatorView.alpha = 0.2
430450
rightUserNotificationAction = actions[0]
431451
leftUserNotificationAction = actions.count >= 2 ? actions[1] : nil
432452

@@ -450,7 +470,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
450470
}
451471
}
452472

453-
fileprivate func presentView() {
473+
private func presentView() {
454474

455475
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: .beginFromCurrentState, animations: {
456476

@@ -459,8 +479,9 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
459479
self.rightActionButton.alpha = 1
460480
self.singleActionButton.alpha = 1
461481
self.textView.alpha = 1
462-
self.sendButton.alpha = 0.2
482+
self.sendButton.alpha = 1
463483
self.currentHeightContainerLayoutConstraint?.constant = 50
484+
self.heightPullViewLayoutConstraint.constant = 0
464485

465486
self.layoutIfNeeded()
466487

@@ -471,20 +492,22 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
471492
if self.shouldShowTextView {
472493
self.textView.becomeFirstResponder()
473494
}
495+
496+
474497
})
475498
}
476499

477-
fileprivate func moveViewToTop() {
500+
private func moveViewToTop() {
478501

479502
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: .beginFromCurrentState, animations: {
480503

481504
self.topConstraintNotification.constant = 0
482505
self.layoutIfNeeded()
483506

484-
}, completion: nil)
507+
})
485508
}
486509

487-
fileprivate func updateNotificationHeightWithNewTextViewHeight(_ height: CGFloat) {
510+
private func updateNotificationHeightWithNewTextViewHeight(_ height: CGFloat) {
488511

489512
UIView.animate(withDuration: 0.4, animations: {
490513

@@ -495,7 +518,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
495518
})
496519
}
497520

498-
fileprivate func heightForText(_ text: String, width: CGFloat) -> CGFloat {
521+
private func heightForText(_ text: String, width: CGFloat) -> CGFloat {
499522

500523
let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
501524
label.numberOfLines = 0
@@ -514,11 +537,7 @@ class BSForegroundNotificationView: UIView, UITextViewDelegate {
514537

515538
func textViewDidChange(_ textView: UITextView) {
516539

517-
let isEmpty = textView.text.characters.isEmpty
518-
519-
sendButton.alpha = isEmpty ? 0.2 : 1
520-
521-
if isEmpty {
540+
if textView.text.characters.isEmpty {
522541
updateNotificationHeightWithNewTextViewHeight(50)
523542
} else {
524543
updateNotificationHeightWithNewTextViewHeight(max(textView.contentSize.height + 20, 50))

0 commit comments

Comments
 (0)