簡短說明:當鍵盤顯示時,我翻譯了一個按鈕,但點擊按鈕後,它會回彈到原來的位置。爲什麼單擊動畫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