Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

[styling] Supporting custom nib for NMessengerBarView #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ open class NMessengerBarView: InputBarView, UITextViewDelegate, CameraViewDelega
//MARK: IBOutlets
//@IBOutlet for InputBarView
@IBOutlet open weak var inputBarView: UIView!

@IBOutlet open weak var photoPickerButton: UIButton!

//@IBOutlet for send button
@IBOutlet open weak var sendButton: UIButton!
//@IBOutlets NSLayoutConstraint input area view height
Expand All @@ -35,7 +38,10 @@ open class NMessengerBarView: InputBarView, UITextViewDelegate, CameraViewDelega
//CGFloat to the fine the number of rows a user can type
open var numberOfRows:CGFloat = 3
//String as placeholder text in input view
open var inputTextViewPlaceholder: String = "NMessenger"
open var inputTextViewPlaceholder: String =
Bundle.main.localizedString(forKey: "Chat.InputField.PlaceholderText",
value: "NMessenger",
table: nil)
{
willSet(newVal)
{
Expand Down Expand Up @@ -64,6 +70,16 @@ open class NMessengerBarView: InputBarView, UITextViewDelegate, CameraViewDelega
super.init(controller: controller)
loadFromBundle()
}

public required init(controller: NMessengerViewController,
nibName: String,
bundle: Bundle)
{
super.init(controller: controller)

self.loadFrom(bundle: bundle, nibName: nibName)
}

/**
Initialiser the view.
- parameter controller: Must be NMessengerViewController. Sets controller for the view.
Expand All @@ -87,8 +103,23 @@ open class NMessengerBarView: InputBarView, UITextViewDelegate, CameraViewDelega
/**
Loads the view from nib file InputBarView and does intial setup.
*/
fileprivate func loadFromBundle() {
_ = Bundle(for: NMessengerViewController.self).loadNibNamed("NMessengerBarView", owner: self, options: nil)?[0] as! UIView
fileprivate func loadFromBundle()
{
let nmessengerBundle = Bundle(for: NMessengerViewController.self)
let nibName = "NMessengerBarView"


self.loadFrom(bundle: nmessengerBundle, nibName: nibName)
}

fileprivate func loadFrom(bundle: Bundle,
nibName: String)
{
let nibObjects = bundle.loadNibNamed( nibName,
owner: self,
options: nil)
/*let fileOwnerSelf*/ _ = nibObjects?[0] as! UIView

self.addSubview(inputBarView)
inputBarView.frame = self.bounds
textInputView.delegate = self
Expand Down Expand Up @@ -176,9 +207,9 @@ open class NMessengerBarView: InputBarView, UITextViewDelegate, CameraViewDelega
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

textInputViewHeight.constant = newFrame.size.height
self.textInputViewHeight.constant = newFrame.size.height

textInputAreaViewHeight.constant = newFrame.size.height+10
self.textInputAreaViewHeight.constant = newFrame.size.height+10



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="NMessengerBarView" customModule="nMessenger" customModuleProvider="target">
<connections>
<outlet property="InputBarView" destination="iN0-l3-epB" id="cHc-9u-IsO"/>
<outlet property="photoPickerButton" destination="ZY8-eq-qAl" id="iaI-pU-y7K"/>
<outlet property="sendButton" destination="6ZI-Ed-TdT" id="nVB-Vj-6Uw"/>
<outlet property="textInputAreaView" destination="Sm0-IA-kLJ" id="B1P-uO-jF0"/>
<outlet property="textInputAreaViewHeight" destination="yKk-PO-qMv" id="eU8-rF-jyc"/>
Expand Down
57 changes: 43 additions & 14 deletions nMessenger/Source/Messenger/NMessengerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,38 +192,67 @@ open class NMessengerViewController: UIViewController, UITextViewDelegate, NMess
/**
Moves InputBarView up and down accoridng to the location of the keyboard
*/
func keyboardNotification(_ notification: Notification) {
if let userInfo = (notification as NSNotification).userInfo {
func keyboardNotification(_ notification: Notification)
{
if let userInfo = (notification as NSNotification).userInfo
{
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions().rawValue
let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
if endFrame?.origin.y >= UIScreen.main.bounds.size.height {


let newKeyboardLocation = endFrame
let keyboardHeight = newKeyboardLocation?.size.height ?? 0
let inputBarHeight = inputBarView.frame.height

let tabBarHeight = self.tabBarController?.tabBar.frame.size.height ?? 0

let isKeyboardOffScreen =
(newKeyboardLocation?.origin.y >= UIScreen.main.bounds.size.height)

let inputViewShiftOffsetY = keyboardHeight - tabBarHeight

if (isKeyboardOffScreen)
{
self.inputBarBottomSpacing.constant = 0
/*let bottomInset*/ _ = inputBarView.frame.height
self.isKeyboardIsShown = false
} else {
if self.inputBarBottomSpacing.constant==0{
self.inputBarBottomSpacing.constant -= endFrame?.size.height ?? 0.0
}
else
{
let isInputBarHasNoBottomSpacing = (self.inputBarBottomSpacing.constant == 0)

if (isInputBarHasNoBottomSpacing)
{
self.inputBarBottomSpacing.constant -= inputViewShiftOffsetY
/*let bottomInset*/ _ = inputViewShiftOffsetY + inputBarHeight
}
else
{
self.inputBarBottomSpacing.constant = 0
self.inputBarBottomSpacing.constant -= endFrame?.size.height ?? 0.0
self.inputBarBottomSpacing.constant -= inputViewShiftOffsetY
/*let bottomInset*/ _ = inputViewShiftOffsetY + inputBarHeight
}
self.isKeyboardIsShown = true
}

let animationsBlock: () -> Swift.Void =
{
self.view.layoutIfNeeded()
if self.isKeyboardIsShown
{
self.messengerView.scrollToLastMessage(animated: true)
}
}

let noDelay: TimeInterval = 0
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
delay: noDelay,
options: animationCurve,
animations: { self.view.layoutIfNeeded()
if self.isKeyboardIsShown {
self.messengerView.scrollToLastMessage(animated: true)
}
},
animations: animationsBlock,
completion: nil)

}
}

Expand Down