0
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    #define kOFFSET_FOR_KEYBOARD 110.0
    textField.frame.origin.y -= kOFFSET_FOR_KEYBOARD;
    textField.frame.size.height += kOFFSET_FOR_KEYBOARD;
    textField.frame = CGRectMake(textField.frame.origin.x, (textField.frame.origin.y - 230.0), textField.frame.size.width, textField.frame.size.height);

}

Got this code for my textField app in iOS... I plan to have it in a way that, when the keyboard appears, the textfield goes up just on top of the keyboard and when the return key in the keyboard is pressed, the textfield goes back to its original position which is at the bottom of the screen. But I get this error, "Expression is not assignable." How do I solve this? What does the error mean?

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
Drei
  • 19
  • 2
  • This most certainly is a dupe. I've remember answering the same question a few weeks ago. –  Jul 31 '13 at 14:40
  • Ahaaaa! Even *that one* was a dupe. Of this one: http://stackoverflow.com/questions/3190639/alter-cgrect-or-any-struct –  Jul 31 '13 at 14:41

2 Answers2

0

You can't assign a value to origin or height. You'll have to assign a CGRect to your frame and update the values in your new CGRect.

JeffRegan
  • 1,322
  • 9
  • 25
0

You cannot edit the frame like this:

textField.frame.origin.y -= kOFFSET_FOR_KEYBOARD;
textField.frame.size.height += kOFFSET_FOR_KEYBOARD;

You need to get the frame, edit it, and then asign it back again, like this:

CGRect myFrame = textField.frame;
myFrame.origin.y -= kOFFSET_FOR_KEYBOARD;
myFrame.size.height += kOFFSET_FOR_KEYBOARD;
textField.frame = myFrame;

If you find yourself having to do this a lot, you can write a category to implement those methods (setHeight, setX...), for example:

#import <UIKit/UIKit.h>

@interface UIView (AlterFrame)

- (void) setFrameWidth:(CGFloat)newWidth;
- (void) setFrameHeight:(CGFloat)newHeight;
- (void) setFrameOriginX:(CGFloat)newX;
- (void) setFrameOriginY:(CGFloat)newY;

@end



#import "UIView+AlterFrame.h"

@implementation UIView (AlterFrame)

    - (void) setFrameWidth:(CGFloat)newWidth {
        CGRect f = self.frame;
        f.size.width = newWidth;
        self.frame = f;
    }

    - (void) setFrameHeight:(CGFloat)newHeight {
        CGRect f = self.frame;
        f.size.height = newHeight;
        self.frame = f;
    }

    - (void) setFrameOriginX:(CGFloat)newX {
        CGRect f = self.frame;
        f.origin.x = newX;
        self.frame = f;
    }

    - (void) setFrameOriginY:(CGFloat)newY {
        CGRect f = self.frame;
        f.origin.y = newY;
        self.frame = f;
    }

@end

Taken from the second responde in:

Alter CGRect (or any struct)?

Community
  • 1
  • 1
Antonio MG
  • 20,382
  • 3
  • 43
  • 62