Skip to content

Commit c6e0c49

Browse files
author
Stephanie Sharp
committed
Replace setError: with setError:animated: to fix layout bug when an error message is displayed on load
1 parent 66449e3 commit c6e0c49

4 files changed

Lines changed: 63 additions & 20 deletions

File tree

MaterialTextField/MFTextField.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,17 @@ IB_DESIGNABLE
6060
*/
6161
@property (nonatomic) IBInspectable UIColor *underlineColor;
6262

63-
/**
63+
/**
6464
* To display an error under the text field, provide an NSError with a localized description.
65+
*
66+
* @param animated Set to YES to animate showing & hiding the error message.
67+
*/
68+
- (void)setError:(NSError *)error animated:(BOOL)animated;
69+
70+
/**
71+
* The error displayed under the text field.
6572
*/
66-
@property (nonatomic) NSError *error;
73+
@property (nonatomic, readonly) NSError *error;
6774

6875
/**
6976
* The font for the error displayed under the text field.

MaterialTextField/MFTextField.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ - (UIFont *)defaultPlaceholderFont
266266

267267
#pragma mark Error
268268

269-
- (void)setError:(NSError *)error
269+
- (void)setError:(NSError *)error animated:(BOOL)animated
270270
{
271271
_error = error;
272-
[self layoutErrorLabelAnimated:YES];
272+
[self layoutErrorLabelAnimated:animated];
273273
}
274274

275275
- (void)setErrorColor:(UIColor *)errorColor

MaterialTextFieldDemo/MaterialTextFieldDemo/Base.lproj/Main.storyboard

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
44
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
66
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
77
</dependencies>
88
<scenes>
@@ -20,7 +20,6 @@
2020
<subviews>
2121
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="some text" placeholder="placeholder" adjustsFontSizeToFit="NO" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Wbu-R9-PKC" customClass="MFTextField">
2222
<rect key="frame" x="20" y="160" width="560" height="40"/>
23-
<animations/>
2423
<fontDescription key="fontDescription" type="system" pointSize="20"/>
2524
<textInputTraits key="textInputTraits"/>
2625
<connections>
@@ -29,7 +28,6 @@
2928
</textField>
3029
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="placeholder" adjustsFontSizeToFit="NO" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="vrI-fI-vIo" customClass="MFTextField">
3130
<rect key="frame" x="20" y="100" width="560" height="40"/>
32-
<animations/>
3331
<constraints>
3432
<constraint firstAttribute="width" constant="280" id="UwL-Iq-bVI"/>
3533
</constraints>
@@ -47,7 +45,6 @@
4745
</textField>
4846
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="line" placeholder="Password" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="A1d-5c-S25" customClass="MFTextField">
4947
<rect key="frame" x="20" y="220" width="560" height="32.5"/>
50-
<animations/>
5148
<fontDescription key="fontDescription" type="system" pointSize="14"/>
5249
<textInputTraits key="textInputTraits" secureTextEntry="YES"/>
5350
<userDefinedRuntimeAttributes>
@@ -64,16 +61,32 @@
6461
</textField>
6562
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="number" textAlignment="right" adjustsFontSizeToFit="NO" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="gNx-E7-aGq" customClass="MFTextField">
6663
<rect key="frame" x="20" y="20" width="560" height="60"/>
67-
<animations/>
6864
<fontDescription key="fontDescription" type="system" pointSize="37"/>
6965
<textInputTraits key="textInputTraits" keyboardType="decimalPad"/>
7066
<connections>
7167
<action selector="textFieldDidChange:" destination="BYZ-38-t0r" eventType="editingChanged" id="WVM-lw-vpg"/>
7268
<outlet property="delegate" destination="BYZ-38-t0r" id="0u6-qT-cZ2"/>
7369
</connections>
7470
</textField>
71+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="placeholder" adjustsFontSizeToFit="NO" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="Z0L-Ka-jzP" customClass="MFTextField">
72+
<rect key="frame" x="20" y="272.5" width="560" height="40"/>
73+
<color key="tintColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
74+
<constraints>
75+
<constraint firstAttribute="width" constant="280" id="goQ-az-rpx"/>
76+
</constraints>
77+
<fontDescription key="fontDescription" type="system" pointSize="20"/>
78+
<textInputTraits key="textInputTraits"/>
79+
<variation key="default">
80+
<mask key="constraints">
81+
<exclude reference="goQ-az-rpx"/>
82+
</mask>
83+
</variation>
84+
<connections>
85+
<action selector="textFieldDidChange:" destination="BYZ-38-t0r" eventType="editingChanged" id="7p3-ay-HPi"/>
86+
<outlet property="delegate" destination="BYZ-38-t0r" id="xHD-DP-ky4"/>
87+
</connections>
88+
</textField>
7589
</subviews>
76-
<animations/>
7790
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
7891
<gestureRecognizers/>
7992
<constraints>
@@ -82,13 +95,16 @@
8295
<constraint firstAttribute="trailingMargin" secondItem="gNx-E7-aGq" secondAttribute="trailing" id="I3m-w6-1eA"/>
8396
<constraint firstItem="gNx-E7-aGq" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="JGp-OO-Lj7"/>
8497
<constraint firstItem="vrI-fI-vIo" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="OcS-24-SuD"/>
98+
<constraint firstItem="Z0L-Ka-jzP" firstAttribute="top" secondItem="A1d-5c-S25" secondAttribute="bottom" constant="20" id="Tsl-Z9-wd4"/>
8599
<constraint firstItem="Wbu-R9-PKC" firstAttribute="top" secondItem="vrI-fI-vIo" secondAttribute="bottom" constant="20" id="TxR-tX-kSq"/>
86100
<constraint firstItem="A1d-5c-S25" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="V7P-NO-erp"/>
87101
<constraint firstAttribute="trailingMargin" secondItem="vrI-fI-vIo" secondAttribute="trailing" id="XRC-Do-j64"/>
88102
<constraint firstItem="vrI-fI-vIo" firstAttribute="top" secondItem="gNx-E7-aGq" secondAttribute="bottom" constant="20" id="YpL-as-BsA"/>
103+
<constraint firstItem="Z0L-Ka-jzP" firstAttribute="trailing" secondItem="8bC-Xf-vdC" secondAttribute="trailingMargin" id="aEg-nt-aHp"/>
89104
<constraint firstItem="Wbu-R9-PKC" firstAttribute="leading" secondItem="vrI-fI-vIo" secondAttribute="leading" id="fuU-6H-anJ"/>
90105
<constraint firstItem="gNx-E7-aGq" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="lwY-Tl-Xmh"/>
91106
<constraint firstItem="A1d-5c-S25" firstAttribute="top" secondItem="Wbu-R9-PKC" secondAttribute="bottom" constant="20" id="sAh-8l-mox"/>
107+
<constraint firstItem="Z0L-Ka-jzP" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="yzG-BW-5DI"/>
92108
</constraints>
93109
<connections>
94110
<outletCollection property="gestureRecognizers" destination="c8Q-pN-pP5" appends="YES" id="9Q4-no-6ng"/>
@@ -97,6 +113,7 @@
97113
<connections>
98114
<outlet property="textField1" destination="gNx-E7-aGq" id="1pI-Mq-qvh"/>
99115
<outlet property="textField2" destination="vrI-fI-vIo" id="e8w-3k-NaW"/>
116+
<outlet property="textField5" destination="Z0L-Ka-jzP" id="lDR-gK-uuK"/>
100117
</connections>
101118
</viewController>
102119
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

MaterialTextFieldDemo/MaterialTextFieldDemo/MaterialTextFieldDemoViewController.m

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ @interface MaterialTextFieldDemoViewController () <UITextFieldDelegate>
1616

1717
@property (weak, nonatomic) IBOutlet MFTextField *textField1;
1818
@property (weak, nonatomic) IBOutlet MFTextField *textField2;
19+
@property (weak, nonatomic) IBOutlet MFTextField *textField5;
1920

2021
@end
2122

@@ -27,6 +28,7 @@ - (void)viewDidLoad
2728

2829
[self setupTextField1];
2930
[self setupTextField2];
31+
[self setupTextField5];
3032
}
3133

3234
#pragma mark - Setup
@@ -51,6 +53,11 @@ - (void)setupTextField2
5153
self.textField2.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Attributed placeholder" attributes:@{NSFontAttributeName:font}];
5254
}
5355

56+
- (void)setupTextField5
57+
{
58+
[self validateTextField5Animated:NO];
59+
}
60+
5461
#pragma mark - Actions
5562

5663
- (IBAction)dismissKeyboard
@@ -59,29 +66,27 @@ - (IBAction)dismissKeyboard
5966
}
6067

6168
- (IBAction)textFieldDidChange:(UITextField *)textField
62-
{
63-
[self updateTextField:textField];
64-
}
65-
66-
#pragma mark - Text field validation
67-
68-
- (void)updateTextField:(UITextField *)textField
6969
{
7070
if (textField == self.textField1) {
7171
[self validateTextField1];
7272
}
7373
else if (textField == self.textField2) {
7474
[self validateTextField2];
7575
}
76+
else if (textField == self.textField5) {
77+
[self validateTextField5Animated:YES];
78+
}
7679
}
7780

81+
#pragma mark - Text field validation
82+
7883
- (void)validateTextField1
7984
{
8085
NSError *error = nil;
8186
if (![self textField1IsValid]) {
8287
error = [self errorWithLocalizedDescription:@"Maximum of 6 characters allowed."];
8388
}
84-
self.textField1.error = error;
89+
[self.textField1 setError:error animated:YES];
8590
}
8691

8792
- (void)validateTextField2
@@ -90,7 +95,16 @@ - (void)validateTextField2
9095
if (![self textField2IsValid]) {
9196
error = [self errorWithLocalizedDescription:@"This is an error message that is really long and should wrap onto 2 or more lines."];
9297
}
93-
self.textField2.error = error;
98+
[self.textField2 setError:error animated:YES];
99+
}
100+
101+
- (void)validateTextField5Animated:(BOOL)animated
102+
{
103+
NSError *error = nil;
104+
if (![self textField5IsValid]) {
105+
error = [self errorWithLocalizedDescription:@"An error message"];
106+
}
107+
[self.textField5 setError:error animated:animated];
94108
}
95109

96110
- (BOOL)textField1IsValid
@@ -103,6 +117,11 @@ - (BOOL)textField2IsValid
103117
return self.textField2.text.length < 3;
104118
}
105119

120+
- (BOOL)textField5IsValid
121+
{
122+
return self.textField5.text.length > 0;
123+
}
124+
106125
- (NSError *)errorWithLocalizedDescription:(NSString *)localizedDescription
107126
{
108127
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: localizedDescription};

0 commit comments

Comments
 (0)