简短说明:当键盘显示时,我翻译了一个按钮,但点击按钮后,它会回弹到原来的位置。为什么单击动画UIButton的位置时会重置?
长描述:我有两个按钮,一个在另一个之上。一个隐藏,直到满足某些标准,然后它变得可见,另一个隐藏。
@property (weak, nonatomic) IBOutlet UIButton *skipButton;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (assign, nonatomic) BOOL saveButtonEnabled;
@property (assign, readonly, nonatomic) CGRect fieldContainerDefaultFrame;
@property (assign, readonly, nonatomic) CGRect skipButtonDefaultFrame;
当视图加载我缓存的缺省位置
- (void)viewDidLoad
{
[super viewDidLoad];
...
_fieldContainerDefaultFrame = self.fieldContainerView.frame;
_skipButtonDefaultFrame = self.skipButton.frame;
[self.saveButton setHidden:YES];
self.saveButtonEnabled = NO;
}
按钮被正确连接到插座,这些都是他们的“润色内部”行动
#pragma mark - Actions
- (IBAction)didPressSaveButton:(id)sender
{
// Code here to persist data
// Code here to push segue to next scene
}
- (IBAction)didPressSkipButton:(id)sender
{
// Code here to push segue to next scene
}
这里有方法响应键盘通知。我注册了通知,我只是不包括该代码。 (有没有的问题出现,所有这些东西被正确运行。)
注:两个按钮都包含在同一个“容器”框(与其他字段一起),换算起来。按钮翻译了额外的金额。翻译一般来说是唯一的问题,就是单击按钮时重置行为。
#pragma mark - Notification handlers
- (void)handleKeyboardWillShowNotification:(NSNotification *)note
{
if([self.bioTextView isFirstResponder])
{
CGRect newFieldContainerFrame = self.fieldContainerDefaultFrame;
CGRect newSkipButtonFrame = self.skipButtonDefaultFrame;
newFieldContainerFrame.origin.y += AGSTSignupDetailsFieldContainerEditingOffset;
newSkipButtonFrame.origin.y += AGSTSignupDetailsSkipButtonEditingOffset;
NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration animations:^{
[UIView setAnimationCurve:animationCurve];
self.fieldContainerView.frame = newFieldContainerFrame;
self.skipButton.frame = newSkipButtonFrame;
self.saveButton.frame = newSkipButtonFrame;
}];
}
}
- (void)handleKeyboardWillHideNotification:(NSNotification *)note
{
NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[UIView animateWithDuration:duration animations:^{
[UIView setAnimationCurve:animationCurve];
self.fieldContainerView.frame = self.fieldContainerDefaultFrame;
self.skipButton.frame = self.skipButtonDefaultFrame;
self.saveButton.frame = self.skipButtonDefaultFrame;
}];
}
我很感激任何关于如何解决错误或如何制定更好的解决方案的见解或建议。谢谢。
您是否使用自动布局?的 –
可能重复的[为什么的UIButton帧由复位的setTitle:forState:方法](http://stackoverflow.com/questions/17997163/why-uibutton-frame-is-reset-by-settitle-forstate-method) –
是,我正在使用自动布局。当我跑出门时,我发布了这个消息,我错过了一些信息。我将在大约十个小时内坐下来彻底解决这个问题。我在想推push segue可能会导致键盘消失,导致平移运行,但随后push segue可能已经取消了所有未决的动画,因为有新的VC进入。但是rob的指针指向约束与帧的问题也很有趣。感谢提示,我今晚会回来。 – bgfriend0