Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • UnderLined TextField/UIView (to prevent unnecessary constraints..)

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 189
    Comment on it
    1. In your xcode project first create a UItextfield type of cocoa touch class in case of underlined view create a uiview type of class as UnderlinedTextField or watever name suits you.

    2. In UnderlinedTextField.h file of your class create a property as-

      @property (nonatomic, retain)CALayer *bottomLayer;

    3. In UnderlinedTextField.m file implement the following code-

        @implementation UnderlinedTextField
        // Only override drawRect: if you perform custom drawing.
        // An empty implementation adversely affects performance during animation.
        - (void)drawRect:(CGRect)rect {
            // Drawing code
            if (!_bottomLayer) {
                _bottomLayer = [CALayer layer];
                _bottomLayer.frame = CGRectMake(0.0f, self.frame.size.height - 1, self.frame.size.width, 1.0f);
                _bottomLayer.backgroundColor = [UIColor grayColor].CGColor;
                [self.layer addSublayer:_bottomLayer];
            }
         }
        - (void)layoutSubviews{
             [super layoutSubviews];
             if (_bottomLayer) {
                CGRect frame = _bottomLayer.frame;
                frame.size.width = self.bounds.size.width;
                _bottomLayer.frame = frame;
            }
    
        }
        @end
    
    1. Now as we have created the underlined textfield/view now we will see how to use them in our ViewController.
    2. 1. First import the UnderlinedTextField.h class in your ViewController. 2. Then simply create outlets of textfields of your Viewcontroller as-
      __weak IBOutlet UnderlinedTextField *usernameTextfield;
      __weak IBOutlet UnderlinedTextField *passwordTextfield;
      
    1. Now Drag & drop uitextfield in your view controller in the Storyboard & set its Border style to first one provided of the four Border styles i.e the dotted one & assign their custom class as UnderlinedTextField
    2. Now you can use your textfields & underline will come with them so the user don't have to set constraints of two separate views instead we can set constraints of only our textfield & it will be displayed underlined.

    Thanks for reading..

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: