programing tip

iPhone의 온 스크린 키보드 높이는 얼마입니까?

itbloger 2020. 9. 20. 09:06
반응형

iPhone의 온 스크린 키보드 높이는 얼마입니까?


세로의 높이와 가로의 높이는 포인트 단위로 측정됩니다.


iOS 7.1에서 키보드 프레임을 결정하기 위해 다음 접근 방식을 사용했습니다.

내 뷰 컨트롤러의 init 메소드에서 UIKeyboardDidShowNotification다음을 등록했습니다 .

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];

그런 다음 다음 코드를 사용 keyboardOnScreen:하여 키보드 프레임에 액세스했습니다. 이 코드는 userInfo알림 에서 사전을 가져온 다음에 NSValue연결된에 액세스합니다 UIKeyboardFrameEndUserInfoKey. 그런 다음 CGRect에 액세스하여 뷰 컨트롤러의 뷰 좌표로 변환 할 수 있습니다. 여기에서 해당 프레임을 기반으로 필요한 모든 계산을 수행 할 수 있습니다.

-(void)keyboardOnScreen:(NSNotification *)notification 
 {
        NSDictionary *info  = notification.userInfo;
        NSValue      *value = info[UIKeyboardFrameEndUserInfoKey];

        CGRect rawFrame      = [value CGRectValue];
        CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];

        NSLog(@"keyboardFrame: %@", NSStringFromCGRect(keyboardFrame));
 }

빠른

그리고 Swift와 동등한 구현 :

NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)


@objc
func keyboardDidShow(notification: Notification) {
    guard let info = notification.userInfo else { return }
    guard let frameInfo = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
    let keyboardFrame = frameInfo.cgRectValue
    print("keyboardFrame: \(keyboardFrame)")
}

iOS 8에서는 화면 키보드의 크기 다를 있습니다. 온 스크린 키보드가 항상 보이 거나 ( 특정 높이로) 보이지 않을 것이라고 가정하지 마십시오 .

이제 iOS 8에서는 사용자가 텍스트 예측 영역을 켜고 끌 수도 있습니다. 이렇게하면 앱 keyboardWillShow이벤트가 다시 시작됩니다 .

이것은 많은 레거시 코드 샘플 깨뜨릴 것입니다 . keyboardWillShow이벤트를 작성 하고 화면 키보드의 현재 높이를 측정하고이 (절대) 양만큼 페이지에서 컨트롤을 위아래로 이동 하도록 권장 합니다.

여기에 이미지 설명 입력

In other words, if you see any sample code, which just tells you to add a keyboardWillShow event, measure the keyboard height, then resize your controls' heights by this amount, this will no longer always work.

In my example above, I used the sample code from the following site, which animates the vertical constraints constant value.

Practicing AutoLayout

In my app, I added a constraint to my UITextView, set to the bottom of the screen. When the screen first appeared, I stored this initial vertical distance.

Then, whenever my keyboardWillShow event gets kicked off, I add the (new) keyboard height to this original constraint value (so the constraint resizes the control's height).

여기에 이미지 설명 입력

Yeah. It's ugly.

And I'm a little annoyed/surprised that XCode 6's horribly-painful AutoLayout doesn't just allow us to attach the bottoms of controls to either the bottom of the screen, or the top of onscreen keyboard.

Perhaps I'm missing something.

Other than my sanity.


Keyboard height is 216pts for portrait mode and 162pts for Landscape mode.

Source


version note: this is no longer value in iOS 9 & 10, as they support custom keyboard sizes.

This depends on the model and the QuickType bar:

여기에 이미지 설명 입력

http://www.idev101.com/code/User_Interface/sizes.html


The keyboard height depends on the model, the QuickType bar, user settings... The best approach is calculate dinamically:

Swift 3.0

    var heightKeyboard : CGFloat?

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    }

    func keyboardShown(notification: NSNotification) {
           if let infoKey  = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
               let rawFrame = (infoKey as AnyObject).cgRectValue {
               let keyboardFrame = view.convert(rawFrame, from: nil)
               self.heightKeyboard = keyboardFrame.size.height
               // Now is stored in your heightKeyboard variable
           }
    }

I can't find latest answer, so I check it all with simulator.(iOS 11.0)


Device | Screen Height | Portrait | Landscape

iPhone 4s | 480.0 | 216.0 | 162.0

iPhone 5, iPhone 5s, iPhone SE | 568.0 | 216.0 | 162.0

iPhone 6, iPhone 6s, iPhone 7, iPhone 8, iPhone X | 667.0 | 216.0 | 162.0

iPhone 6 plus, iPhone 7 plus, iPhone 8 plus | 736.0 | 226.0 | 162.0

iPad 5th generation, iPad Air, iPad Air 2, iPad Pro 9.7, iPad Pro 10.5, iPad Pro 12.9 | 1024.0 | 265.0 | 353.0


Thanks!


iPhone

KeyboardSizes:

  1. 5S, SE, 5, 5C (320 × 568) keyboardSize = (0.0, 352.0, 320.0, 216.0) keyboardSize = (0.0, 315.0, 320.0, 253.0)

2.6S,6,7,8:(375 × 667) : keyboardSize = (0.0, 407.0, 375.0, 260.

3.6+,6S+, 7+ , 8+ : (414 × 736) keyboardSize = (0.0, 465.0, 414.0, 271.0)

4.XS, X :(375 X 812) keyboardSize = (0.0, 477.0, 375.0, 335.0)

5.XR,XSMAX((414 x 896) keyboardSize = (0.0, 550.0, 414.0, 346.0)

참고URL : https://stackoverflow.com/questions/11284321/what-is-the-height-of-iphones-onscreen-keyboard

반응형