Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Fixes crash caused by calling layoutIfNeeded too early, before UITa…
Browse files Browse the repository at this point in the history
…bleView or UICollectionView sets up its datasource
  • Loading branch information
Ignacio Romero Zurbuchen authored and Ignacio Romero committed Sep 10, 2015
1 parent e069c18 commit dceedc7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Examples/Messenger-Shared/MessageTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@implementation MessageTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
Expand Down
30 changes: 9 additions & 21 deletions Source/Classes/SLKTextInputbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ - (void)slk_commonInit

- (void)layoutIfNeeded
{
if (self.constraints.count == 0) {
if (self.constraints.count == 0 || !self.window) {
return;
}

Expand Down Expand Up @@ -274,22 +274,6 @@ - (UILabel *)charCountLabel
return _charCountLabel;
}

- (BOOL)isViewVisible
{
SEL selector = NSSelectorFromString(@"isViewVisible");

if ([self.controller respondsToSelector:selector]) {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
BOOL visible = (BOOL)[self.controller performSelector:selector];
#pragma clang diagnostic pop

return visible;
}
return NO;
}

- (CGFloat)minimumInputbarHeight
{
CGFloat minimumTextViewHeight = self.textView.intrinsicContentSize.height;
Expand Down Expand Up @@ -329,7 +313,7 @@ - (CGFloat)slk_inputBarHeightForLines:(NSUInteger)numberOfLines
CGFloat height = self.textView.intrinsicContentSize.height;
height -= self.textView.font.lineHeight;
height += roundf(self.textView.font.lineHeight*numberOfLines);
height += self.contentInset.top+self.contentInset.bottom;
height += self.contentInset.top + self.contentInset.bottom;

return height;
}
Expand All @@ -355,13 +339,15 @@ - (CGFloat)slk_appropriateRightButtonWidth
NSString *title = [self.rightButton titleForState:UIControlStateNormal];

CGSize rightButtonSize;

if ([title length] == 0 && self.rightButton.imageView.image) {
rightButtonSize = self.rightButton.imageView.image.size;
} else {
}
else {
rightButtonSize = [title sizeWithAttributes:@{NSFontAttributeName: self.rightButton.titleLabel.font}];
}

return rightButtonSize.width+self.contentInset.right;
return rightButtonSize.width + self.contentInset.right;
}

- (CGFloat)slk_appropriateRightButtonMargin
Expand Down Expand Up @@ -405,6 +391,7 @@ - (void)setAutoHideRightButton:(BOOL)hide
_autoHideRightButton = hide;

self.rightButtonWC.constant = [self slk_appropriateRightButtonWidth];

[self layoutIfNeeded];
}

Expand Down Expand Up @@ -507,6 +494,7 @@ - (void)endTextEdition
}

self.editing = NO;

[self slk_updateConstraintConstants];
}

Expand Down Expand Up @@ -570,7 +558,7 @@ - (void)slk_didChangeTextViewText:(NSNotification *)notification

BOOL bounces = self.controller.bounces && [self.textView isFirstResponder];

if ([self isViewVisible]) {
if (self.window) {
[self slk_animateLayoutIfNeededWithBounce:bounces
options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState
animations:NULL];
Expand Down
9 changes: 9 additions & 0 deletions Source/Classes/SLKTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,15 @@ - (void)setTypingSuggestionEnabled:(BOOL)enabled

#pragma mark - UITextView Overrides

- (void)layoutIfNeeded
{
if (!self.window) {
return;
}

[super layoutIfNeeded];
}

- (NSArray *)gestureRecognizers
{
NSArray *gestureRecognizers = [super gestureRecognizers];
Expand Down

0 comments on commit dceedc7

Please sign in to comment.