iOS 8 点击 UITextView 冻结应用程序
iOS 8 Tap on UITextView freezes the app
我有一个旧代码,没有 ARC,没有自动布局,我正在以编程方式构建 UITableViewCell
。 UITableViewCell
包含一个UITextView
。单元格构造正确,但是当我点击 UITextView
进行编辑时,应用程序冻结并且在一段时间后它存在(我认为这是因为它花费了很多 CPU 时间)。这只发生在 iOS 8,其他版本它工作正常。
我进行了调试,确实收到了UIKeyboardWillShowNotification
通知,但是键盘没有出现,之后我什么也做不了。
下面是代码
// Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
CGRect frame = CGRectMake(MIN_X,(LABEL_HEIGHT + VERTICAL_PADDING) + 2,TEXT_BOX_WIDTH,TEXT_BOX_HEIGHT);
self.roundRectView = [[RoundRectView alloc]initWithFrame:frame];
self.roundRectView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:roundRectView];
inputTextView = [[UITextView alloc] initWithFrame:frame];
inputTextView.editable = YES;
inputTextView.delegate = self;
if(self.parameter.selectedValue == nil || [self.parameter.selectedValue length] == 0){
inputTextView.text = self.parameter.label;
}
else{
inputTextView.text = self.parameter.selectedValue;
}
inputTextView.backgroundColor = [UIColor clearColor];
inputTextView.textAlignment = UITextAlignmentLeft;
inputTextView.font = [UIFont fontWithName:@"HelveticaNeue" size:15.0];
inputTextView.textColor = [UIColor grayColor];
inputTextView.scrollEnabled = NO;
inputTextView.bounces = NO;
inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[self.contentView addSubview:inputTextView];
下面是UIKeyboardWillShowNotification
处理程序:-
-(void) keyboardWillShow:(NSNotification *)note{
if([self.inputTextView isFirstResponder]) {
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.parentTableView.frame;
// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// Reduce size of the Table view
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
frame.size.height -= keyboardBounds.size.height-100;
else
frame.size.height -= keyboardBounds.size.width-100;
// Apply new size of table view
self.parentTableView.frame = frame;
// Scroll the table view to see the TextField just above the keyboard
if (self.inputTextView)
{
CGRect textFieldRect = [self.parentTableView convertRect:self.inputTextView.bounds fromView:self.inputTextView];
[self.parentTableView scrollRectToVisible:textFieldRect animated:NO];
}
[UIView commitAnimations];
}}
以下是崩溃报告。我的应用名称是 "My App"
Exception Type: 00000020
Exception Codes: 0x000000008badf00d
Highlighted Thread: 0
Application Specific Information:
com.myapp.app failed to exit in time
Elapsed total CPU time (seconds): 8.070 (user 8.070, system 0.000), 66% CPU
Elapsed application CPU time (seconds): 5.689, 47% CPU
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0:
0 QuartzCore 0x27e013ca x_hash_table_lookup + 62
1 QuartzCore 0x27e10c60 CAMediaTimingCopyRenderTiming + 120
2 QuartzCore 0x27e0fd4a CA::Layer::add_animation(CAAnimation*, __CFString const*) + 650
3 QuartzCore 0x27e0f876 -[CALayer addAnimation:forKey:] + 478
4 UIKit 0x28673c4c __67-[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:]_block_invoke + 376
5 UIKit 0x28673a98 -[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:] + 544
6 QuartzCore 0x27e02788 CA::Layer::end_change(CA::Transaction*, unsigned int, objc_object*) + 92
7 QuartzCore 0x27e03322 CA::Layer::set_bounds(CA::Rect const&, bool) + 522
8 QuartzCore 0x27e03046 -[CALayer setBounds:] + 106
9 QuartzCore 0x27e0407a -[CALayer setFrame:] + 538
10 UIKit 0x283dcfde -[UIView(Geometry) setFrame:] + 250
11 UIKit 0x283f7408 -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize:] + 804
12 UIKit 0x283f70d2 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 254
13 CoreFoundation 0x24e6506e __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 86
14 CoreFoundation 0x24e64f68 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 200
15 UIKit 0x283e7bea -[UIView(Geometry) resizeSubviewsWithOldSize:] + 114
16 UIKit 0x283dd0f2 -[UIView(Geometry) setFrame:] + 526
17 UIKit 0x28469320 -[UILayoutContainerView setFrame:] + 56
18 UIKit 0x283f7408 -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize:] + 804
19 UIKit 0x283f70d2 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 254
20 CoreFoundation 0x24e6506e __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 86
21 CoreFoundation 0x24e64f68 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 200
22 UIKit 0x283e7bea -[UIView(Geometry) resizeSubviewsWithOldSize:] + 114
23 UIKit 0x28429b40 -[UIView(Geometry) setBounds:] + 476
24 UIKit 0x288b7ad8 -[UIDropShadowView setBounds:] + 124
25 UIKit 0x287cdf84 -[_UIFormSheetPresentationController containerViewWillLayoutSubviews] + 272
26 UIKit 0x283e0154 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 440
27 QuartzCore 0x27e0bf60 -[CALayer layoutSublayers] + 132
28 QuartzCore 0x27e0794c CA::Layer::layout_if_needed(CA::Transaction*) + 356
29 UIKit 0x283f28e6 -[UIView(Hierarchy) layoutBelowIfNeeded] + 134
30 UIKit 0x28413d22 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 474
31 UIKit 0x28413b3e +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:] + 62
32 UIKit 0x287cdb44 -[_UIFormSheetPresentationController _changeLayoutModeToAvoidKeyboard:] + 220
33 CoreFoundation 0x24ef6f12 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 10
34 CoreFoundation 0x24e51e48 _CFXNotificationPost + 1796
35 Foundation 0x25b86e5c -[NSNotificationCenter postNotificationName:object:userInfo:] + 72
36 UIKit 0x28a44b94 -[UIInputWindowController postStartNotifications:withInfo:] + 412
37 UIKit 0x28a45fb4 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke579 + 396
38 UIKit 0x28413d22 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 474
39 UIKit 0x284d579e +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 62
40 UIKit 0x284a093a -[UIInputViewAnimationStyle launchAnimation:afterStarted:completion:forHost:fromCurrentPosition:] + 166
41 UIKit 0x28a45d18 -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 828
42 UIKit 0x28a49584 -[UIInputWindowController setPlacement:starting:completion:] + 44
43 UIKit 0x28a4913a -[UIInputWindowController setInputViewSet:] + 610
44 UIKit 0x28a457aa -[UIInputWindowController performOperations:withAnimationStyle:] + 38
45 UIKit 0x28498102 -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1010
46 UIKit 0x2845b7f0 -[UIResponder becomeFirstResponder] + 404
47 UIKit 0x2845bab6 -[UIView(Hierarchy) becomeFirstResponder] + 102
48 UIKit 0x2857ab6a -[UITextView becomeFirstResponder] + 70
49 UIKit 0x2857aa6a -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 170
50 UIKit 0x2857a36a -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 1994
51 UIKit 0x285601aa _UIGestureRecognizerSendActions + 290
52 UIKit 0x2840ec98 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 664
53 UIKit 0x28823a8c ___UIGestureRecognizerUpdate_block_invoke661 + 44
54 UIKit 0x283d8512 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 214
55 UIKit 0x283d648a _UIGestureRecognizerUpdate + 2946
56 UIKit 0x2840d088 -[UIWindow _sendGesturesForEvent:] + 780
57 UIKit 0x2840c958 -[UIWindow sendEvent:] + 516
58 UIKit 0x283e30e0 -[UIApplication sendEvent:] + 192
59 My App 0x0023e95a 0x28000 + 2189658
60 UIKit 0x28656b9e _UIApplicationHandleEventFromQueueEvent + 13870
61 UIKit 0x283e1b22 _UIApplicationHandleEventQueue + 1290
62 CoreFoundation 0x24f04fd4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
63 CoreFoundation 0x24f043e6 __CFRunLoopDoSources0 + 218
64 CoreFoundation 0x24f02a64 __CFRunLoopRun + 764
65 CoreFoundation 0x24e4fb2c CFRunLoopRunSpecific + 472
66 CoreFoundation 0x24e4f93e CFRunLoopRunInMode + 102
67 GraphicsServices 0x2c1d504c GSEventRunModal + 132
68 UIKit 0x28441f1c UIApplicationMain + 1436
69 My App 0x0002e56e 0x28000 + 25966
70 My App 0x0002e4f4 0x28000 + 25844
如果有人遇到同样的问题,请发布我的解决方案。
TextView 位于一个视图控制器中,该视图控制器使用 UIModalPresentationFormSheet 呈现样式以模态方式呈现。在我更改为 UIModalPresentationPageSheet 后,它工作正常。这意味着表单 sheet 在弹出键盘时导致了一些问题。
我有一个旧代码,没有 ARC,没有自动布局,我正在以编程方式构建 UITableViewCell
。 UITableViewCell
包含一个UITextView
。单元格构造正确,但是当我点击 UITextView
进行编辑时,应用程序冻结并且在一段时间后它存在(我认为这是因为它花费了很多 CPU 时间)。这只发生在 iOS 8,其他版本它工作正常。
我进行了调试,确实收到了UIKeyboardWillShowNotification
通知,但是键盘没有出现,之后我什么也做不了。
下面是代码
// Register notification when the keyboard will be show
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// Register notification when the keyboard will be hide
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
CGRect frame = CGRectMake(MIN_X,(LABEL_HEIGHT + VERTICAL_PADDING) + 2,TEXT_BOX_WIDTH,TEXT_BOX_HEIGHT);
self.roundRectView = [[RoundRectView alloc]initWithFrame:frame];
self.roundRectView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:roundRectView];
inputTextView = [[UITextView alloc] initWithFrame:frame];
inputTextView.editable = YES;
inputTextView.delegate = self;
if(self.parameter.selectedValue == nil || [self.parameter.selectedValue length] == 0){
inputTextView.text = self.parameter.label;
}
else{
inputTextView.text = self.parameter.selectedValue;
}
inputTextView.backgroundColor = [UIColor clearColor];
inputTextView.textAlignment = UITextAlignmentLeft;
inputTextView.font = [UIFont fontWithName:@"HelveticaNeue" size:15.0];
inputTextView.textColor = [UIColor grayColor];
inputTextView.scrollEnabled = NO;
inputTextView.bounces = NO;
inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[self.contentView addSubview:inputTextView];
下面是UIKeyboardWillShowNotification
处理程序:-
-(void) keyboardWillShow:(NSNotification *)note{
if([self.inputTextView isFirstResponder]) {
// Get the keyboard size
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];
// Detect orientation
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect frame = self.parentTableView.frame;
// Start animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// Reduce size of the Table view
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
frame.size.height -= keyboardBounds.size.height-100;
else
frame.size.height -= keyboardBounds.size.width-100;
// Apply new size of table view
self.parentTableView.frame = frame;
// Scroll the table view to see the TextField just above the keyboard
if (self.inputTextView)
{
CGRect textFieldRect = [self.parentTableView convertRect:self.inputTextView.bounds fromView:self.inputTextView];
[self.parentTableView scrollRectToVisible:textFieldRect animated:NO];
}
[UIView commitAnimations];
}}
以下是崩溃报告。我的应用名称是 "My App"
Exception Type: 00000020
Exception Codes: 0x000000008badf00d
Highlighted Thread: 0
Application Specific Information:
com.myapp.app failed to exit in time
Elapsed total CPU time (seconds): 8.070 (user 8.070, system 0.000), 66% CPU
Elapsed application CPU time (seconds): 5.689, 47% CPU
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0:
0 QuartzCore 0x27e013ca x_hash_table_lookup + 62
1 QuartzCore 0x27e10c60 CAMediaTimingCopyRenderTiming + 120
2 QuartzCore 0x27e0fd4a CA::Layer::add_animation(CAAnimation*, __CFString const*) + 650
3 QuartzCore 0x27e0f876 -[CALayer addAnimation:forKey:] + 478
4 UIKit 0x28673c4c __67-[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:]_block_invoke + 376
5 UIKit 0x28673a98 -[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:] + 544
6 QuartzCore 0x27e02788 CA::Layer::end_change(CA::Transaction*, unsigned int, objc_object*) + 92
7 QuartzCore 0x27e03322 CA::Layer::set_bounds(CA::Rect const&, bool) + 522
8 QuartzCore 0x27e03046 -[CALayer setBounds:] + 106
9 QuartzCore 0x27e0407a -[CALayer setFrame:] + 538
10 UIKit 0x283dcfde -[UIView(Geometry) setFrame:] + 250
11 UIKit 0x283f7408 -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize:] + 804
12 UIKit 0x283f70d2 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 254
13 CoreFoundation 0x24e6506e __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 86
14 CoreFoundation 0x24e64f68 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 200
15 UIKit 0x283e7bea -[UIView(Geometry) resizeSubviewsWithOldSize:] + 114
16 UIKit 0x283dd0f2 -[UIView(Geometry) setFrame:] + 526
17 UIKit 0x28469320 -[UILayoutContainerView setFrame:] + 56
18 UIKit 0x283f7408 -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize:] + 804
19 UIKit 0x283f70d2 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 254
20 CoreFoundation 0x24e6506e __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 86
21 CoreFoundation 0x24e64f68 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 200
22 UIKit 0x283e7bea -[UIView(Geometry) resizeSubviewsWithOldSize:] + 114
23 UIKit 0x28429b40 -[UIView(Geometry) setBounds:] + 476
24 UIKit 0x288b7ad8 -[UIDropShadowView setBounds:] + 124
25 UIKit 0x287cdf84 -[_UIFormSheetPresentationController containerViewWillLayoutSubviews] + 272
26 UIKit 0x283e0154 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 440
27 QuartzCore 0x27e0bf60 -[CALayer layoutSublayers] + 132
28 QuartzCore 0x27e0794c CA::Layer::layout_if_needed(CA::Transaction*) + 356
29 UIKit 0x283f28e6 -[UIView(Hierarchy) layoutBelowIfNeeded] + 134
30 UIKit 0x28413d22 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 474
31 UIKit 0x28413b3e +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:] + 62
32 UIKit 0x287cdb44 -[_UIFormSheetPresentationController _changeLayoutModeToAvoidKeyboard:] + 220
33 CoreFoundation 0x24ef6f12 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 10
34 CoreFoundation 0x24e51e48 _CFXNotificationPost + 1796
35 Foundation 0x25b86e5c -[NSNotificationCenter postNotificationName:object:userInfo:] + 72
36 UIKit 0x28a44b94 -[UIInputWindowController postStartNotifications:withInfo:] + 412
37 UIKit 0x28a45fb4 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke579 + 396
38 UIKit 0x28413d22 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 474
39 UIKit 0x284d579e +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 62
40 UIKit 0x284a093a -[UIInputViewAnimationStyle launchAnimation:afterStarted:completion:forHost:fromCurrentPosition:] + 166
41 UIKit 0x28a45d18 -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 828
42 UIKit 0x28a49584 -[UIInputWindowController setPlacement:starting:completion:] + 44
43 UIKit 0x28a4913a -[UIInputWindowController setInputViewSet:] + 610
44 UIKit 0x28a457aa -[UIInputWindowController performOperations:withAnimationStyle:] + 38
45 UIKit 0x28498102 -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1010
46 UIKit 0x2845b7f0 -[UIResponder becomeFirstResponder] + 404
47 UIKit 0x2845bab6 -[UIView(Hierarchy) becomeFirstResponder] + 102
48 UIKit 0x2857ab6a -[UITextView becomeFirstResponder] + 70
49 UIKit 0x2857aa6a -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 170
50 UIKit 0x2857a36a -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 1994
51 UIKit 0x285601aa _UIGestureRecognizerSendActions + 290
52 UIKit 0x2840ec98 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 664
53 UIKit 0x28823a8c ___UIGestureRecognizerUpdate_block_invoke661 + 44
54 UIKit 0x283d8512 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 214
55 UIKit 0x283d648a _UIGestureRecognizerUpdate + 2946
56 UIKit 0x2840d088 -[UIWindow _sendGesturesForEvent:] + 780
57 UIKit 0x2840c958 -[UIWindow sendEvent:] + 516
58 UIKit 0x283e30e0 -[UIApplication sendEvent:] + 192
59 My App 0x0023e95a 0x28000 + 2189658
60 UIKit 0x28656b9e _UIApplicationHandleEventFromQueueEvent + 13870
61 UIKit 0x283e1b22 _UIApplicationHandleEventQueue + 1290
62 CoreFoundation 0x24f04fd4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
63 CoreFoundation 0x24f043e6 __CFRunLoopDoSources0 + 218
64 CoreFoundation 0x24f02a64 __CFRunLoopRun + 764
65 CoreFoundation 0x24e4fb2c CFRunLoopRunSpecific + 472
66 CoreFoundation 0x24e4f93e CFRunLoopRunInMode + 102
67 GraphicsServices 0x2c1d504c GSEventRunModal + 132
68 UIKit 0x28441f1c UIApplicationMain + 1436
69 My App 0x0002e56e 0x28000 + 25966
70 My App 0x0002e4f4 0x28000 + 25844
如果有人遇到同样的问题,请发布我的解决方案。
TextView 位于一个视图控制器中,该视图控制器使用 UIModalPresentationFormSheet 呈现样式以模态方式呈现。在我更改为 UIModalPresentationPageSheet 后,它工作正常。这意味着表单 sheet 在弹出键盘时导致了一些问题。