Skip to content

Commit ce963c9

Browse files
VincentSitdzenbot
authored and
dzenbot
committed
Move some code to viewdidload & dynamic update leftButton size.
1 parent 1eaca1e commit ce963c9

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Source/Classes/SLKTextInputbar.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ - (void)commonInit
6464
[self setupViewConstraints];
6565

6666
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeTextView:) name:UITextViewTextDidChangeNotification object:nil];
67+
[self.leftButton.imageView addObserver:self forKeyPath:NSStringFromSelector(@selector(image)) options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
6768
}
6869

6970

@@ -460,7 +461,7 @@ - (void)updateConstraintConstants
460461
{
461462
self.accessoryViewHC.constant = zero;
462463

463-
CGSize leftButtonSize = [self.leftButton imageForState:UIControlStateNormal].size;
464+
CGSize leftButtonSize = [self.leftButton imageForState:self.leftButton.state].size;
464465

465466
self.leftButtonWC.constant = roundf(leftButtonSize.width);
466467
self.leftButtonHC.constant = roundf(leftButtonSize.height);
@@ -472,12 +473,30 @@ - (void)updateConstraintConstants
472473
}
473474
}
474475

476+
#pragma mark - Observers
477+
478+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
479+
{
480+
if ([object isEqual:self.leftButton.imageView] && [keyPath isEqualToString:NSStringFromSelector(@selector(image))]) {
481+
UIImage *newImage = change[NSKeyValueChangeNewKey];
482+
UIImage *oldImage = change[NSKeyValueChangeOldKey];
483+
if ([newImage isEqual:oldImage]) {
484+
return;
485+
}
486+
[self updateConstraintConstants];
487+
}
488+
else {
489+
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
490+
}
491+
}
492+
475493

476494
#pragma mark - Lifeterm
477495

478496
- (void)dealloc
479497
{
480498
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
499+
[self removeObserver:self.leftButton.imageView forKeyPath:NSStringFromSelector(@selector(image))];
481500

482501
_leftButton = nil;
483502
_rightButton = nil;

Source/Classes/SLKTextViewController.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ - (void)commonInit
8181
self.inverted = YES;
8282
self.undoShakingEnabled = NO;
8383
self.keyboardPanningEnabled = YES;
84+
}
85+
86+
87+
#pragma mark - View lifecycle
88+
89+
- (void)viewDidLoad
90+
{
91+
[super viewDidLoad];
8492

8593
self.view.backgroundColor = [UIColor whiteColor];
8694

@@ -92,9 +100,6 @@ - (void)commonInit
92100
[self setupViewConstraints];
93101
}
94102

95-
96-
#pragma mark - View lifecycle
97-
98103
- (void)viewWillAppear:(BOOL)animated
99104
{
100105
[super viewWillAppear:animated];

0 commit comments

Comments
 (0)