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

Center right button and update constraints when its size is changed #186

Merged
merged 2 commits into from
Aug 7, 2015
Merged
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
23 changes: 19 additions & 4 deletions Source/Classes/SLKTextInputbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ @interface SLKTextInputbar ()
@property (nonatomic, strong) NSLayoutConstraint *bottomMarginWC;
@property (nonatomic, strong) NSLayoutConstraint *rightButtonWC;
@property (nonatomic, strong) NSLayoutConstraint *rightMarginWC;
@property (nonatomic, strong) NSLayoutConstraint *rightButtonTopMarginC;
@property (nonatomic, strong) NSLayoutConstraint *rightButtonBottomMarginC;
@property (nonatomic, strong) NSLayoutConstraint *editorContentViewHC;
@property (nonatomic, strong) NSArray *charCountLabelVCs;

Expand Down Expand Up @@ -92,6 +94,7 @@ - (void)slk_commonInit
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slk_didChangeTextViewContentSize:) name:SLKTextViewContentSizeDidChangeNotification object:nil];

[self.leftButton.imageView addObserver:self forKeyPath:NSStringFromSelector(@selector(image)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
[self.rightButton.titleLabel addObserver:self forKeyPath:NSStringFromSelector(@selector(font)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
}


Expand Down Expand Up @@ -535,11 +538,8 @@ - (void)slk_didChangeTextViewContentSize:(NSNotification *)notification

- (void)slk_setupViewConstraints
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove these?
This is still necessary to detect and adjust the left button.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they needed there? The leftVerMargin generated from the UIImage size is not used when the constraints are set. It looks like the real adjustment is made in slk_updateConstraintConstants. Not sure, this was something I noticed while implementing the proper fix, not really needed for it to work.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It's no longer used here anymore.

{
UIImage *leftButtonImg = [self.leftButton imageForState:UIControlStateNormal];

[self.rightButton sizeToFit];

CGFloat leftVerMargin = (self.intrinsicContentSize.height - leftButtonImg.size.height) / 2.0;
CGFloat rightVerMargin = (self.intrinsicContentSize.height - CGRectGetHeight(self.rightButton.frame)) / 2.0;

NSDictionary *views = @{@"textView": self.textView,
Expand All @@ -553,7 +553,6 @@ - (void)slk_setupViewConstraints
@"bottom" : @(self.contentInset.bottom),
@"left" : @(self.contentInset.left),
@"right" : @(self.contentInset.right),
@"leftVerMargin" : @(leftVerMargin),
@"rightVerMargin" : @(rightVerMargin),
@"minTextViewHeight" : @(self.textView.intrinsicContentSize.height),
};
Expand All @@ -576,6 +575,9 @@ - (void)slk_setupViewConstraints

self.rightButtonWC = [self slk_constraintForAttribute:NSLayoutAttributeWidth firstItem:self.rightButton secondItem:nil];
self.rightMarginWC = [self slk_constraintsForAttribute:NSLayoutAttributeTrailing][0];

self.rightButtonTopMarginC = [self slk_constraintForAttribute:NSLayoutAttributeTop firstItem:self.rightButton secondItem:self];
self.rightButtonBottomMarginC = [self slk_constraintForAttribute:NSLayoutAttributeBottom firstItem:self secondItem:self.rightButton];
}

- (void)slk_updateConstraintConstants
Expand Down Expand Up @@ -607,6 +609,13 @@ - (void)slk_updateConstraintConstants

self.rightButtonWC.constant = [self slk_appropriateRightButtonWidth];
self.rightMarginWC.constant = [self slk_appropriateRightButtonMargin];

[self.rightButton sizeToFit];

CGFloat rightVerMargin = (self.intrinsicContentSize.height - CGRectGetHeight(self.rightButton.frame)) / 2.0;

self.rightButtonTopMarginC.constant = rightVerMargin;
self.rightButtonBottomMarginC.constant = rightVerMargin;
}
}

Expand All @@ -624,6 +633,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

[self slk_updateConstraintConstants];
}
else if ([object isEqual:self.rightButton.titleLabel] && [keyPath isEqualToString:NSStringFromSelector(@selector(font))]) {
[self slk_updateConstraintConstants];
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
Expand All @@ -638,6 +650,7 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self name:SLKTextViewContentSizeDidChangeNotification object:nil];

[_leftButton.imageView removeObserver:self forKeyPath:NSStringFromSelector(@selector(image))];
[_rightButton.titleLabel removeObserver:self forKeyPath:NSStringFromSelector(@selector(font))];

_leftButton = nil;
_rightButton = nil;
Expand All @@ -656,6 +669,8 @@ - (void)dealloc
_bottomMarginWC = nil;
_rightButtonWC = nil;
_rightMarginWC = nil;
_rightButtonTopMarginC = nil;
_rightButtonBottomMarginC = nil;
_editorContentViewHC = nil;
}

Expand Down