1. 程式人生 > >UIScrollView 固定滾動方向

UIScrollView 固定滾動方向

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;

    // 限制y軸不動
    point.x = 0.f;

    scrollView.contentOffset = point;
}

配合ABKeyboardManager 使用

    UIScrollView *sv = (UIScrollView*)self.view;
    sv.delegate = self;
        ABKeyboardManager *keyboard = [ABKeyboardManager  sharedManager];
// [keyboard.scrollView setContentOffset:CGPointMake(0, 0)];y = 0 代表y 不動 // [keyboard.scrollView setContentSize:self.view.bounds.size]; keyboard.keyboardDistanceFromTextField = 100; keyboard.scrollView =sv; [keyboard startTextFieldObserve:self];

//
//let tap = UITapGestureRecognizer(target: self, action: #selector(tapMe))
//self.view.addGestureRecognizer(tap) //func tapMe() { // self.scrollView.contentOffset = CGPoint(x: 0, y: 0) // self.userTF.resignFirstResponder() // self.pwTF.resignFirstResponder() //} //自己實現點選檢視的任何地方鍵盤訊息 #pragma mark - ******** 還原之前的移動 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; UIScrollView
*sv = (UIScrollView*)self.view; sv.contentOffset = CGPointMake(0, 0); return true; }

鍵盤隱藏之後 就要還原檢視的到原來的座標

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - ******** 鍵盤隱藏之後 就要還原

- (void) keyboardWillHide : (NSNotification*)notification {
    CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; //獲得鍵盤的rect
    //通過rect做響應的彈起等
    UIScrollView *sv = (UIScrollView*)self.view;
    sv.contentOffset = CGPointMake(0, 0);
}