UITextView是模态控制器视图的子视图。我需要在键盘出现时降低UITextView高度,以使UITextView的底部边框y坐标等于键盘的顶部y坐标。 我想要键盘高度
CGRect frameBegin = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] ;
CGRect frameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect resultBegin = [self.view convertRect:frameBegin fromView:nil];
CGRect resultEnd = [self.view convertRect:frameEnd fromView:nil];
CGFloat kbdHeight = resultBegin.origin.y - resultEnd.origin.y;
问题是当键盘出现时,此模态视图会跳起来。在这种情况下如何计算键盘的顶部边框坐标?
答案 0 :(得分:4)
你可以这样做:
1. Register for keyboard notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myTextViewHeightAdjustMethod:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myTextViewHeightAdjustMethod:)
name:UIKeyboardDidShowNotification
object:nil];
2. Calculate intersection and adjust textView height with the bottom constraint
- (void)myTextViewHeightAdjustMethod:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGRect keyboardFinalFrame = [[userInfo emf_ObjectOrNilForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGPoint keyboardOriginInView = [self.view convertPoint:keyboardFinalFrame.origin fromView:nil];
CGFloat intersectionY = CGRectGetMaxY(self.view.frame) - keyboardOriginInView.y;
if (intersectionY >= 0)
{
self.textViewBottomConstraint.constant = intersectionY + originalTextViewBottomConstraint;
[self.textView setNeedsLayout];
}
请记得取消注册通知。
答案 1 :(得分:0)
如果您不需要自己编写代码,我建议您使用https://github.com/hackiftekhar/IQKeyboardManager
它很棒(对我来说,到目前为止),你需要做的就是导入它并在AppDelegate中添加这一行代码:
let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: self.player.currentItem!.asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
let t : CMTime!
let videoLengthInSeconds = CMTimeGetSeconds(self.player.currentItem!.asset.duration)
t = CMTimeMakeWithSeconds(Float64(videoLengthInSeconds) * Float64(0.5), 60000)
do {
let img : CGImageRef = try assetImgGenerate.copyCGImageAtTime(t, actualTime: nil)
let frameImg : UIImage = UIImage(CGImage: img)
return frameImg
} catch let error as NSError {
print("fucking nil")
print(error)
} catch {
debugPrint("Generic error")
}