表达式不是可分配的错误

时间:2013-07-31 14:24:23

标签: ios uitextfielddelegate

- (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);

}

在iOS中为我的textField应用程序获取此代码...我计划以一种方式使用,当键盘出现时,文本字段在键盘顶部上升,当按下键盘中的返回键时,文本字段返回到屏幕底部的原始位置。但我得到这个错误,“表达式不可分配。”我该如何解决这个问题?错误是什么意思?

2 个答案:

答案 0 :(得分:0)

您无法为原点或高度指定值。您必须为帧分配CGRect并更新新CGRect中的值。

答案 1 :(得分:0)

您不能像这样编辑框架:

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

您需要获取框架,进行编辑,然后再将其重新标记,如下所示:

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

如果你发现自己不得不这么做,你可以编写一个类别来实现这些方法(setHeight,setX ...),例如:

#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

取自第二回应:

Alter CGRect (or any struct)?