ios - 更改容器高度后 UIButton 不可点击

标签 ios objective-c

UIButton 包含在 UIScrollView 中。我在xib中有这个设置。我使用 NSLayoutConstraint 来更改 ScrollView 的高度。单击按钮后, ScrollView 会更改高度,但随后 UIButton 变得不可单击。

这是代码:

- (IBAction)TagPressed:(UIButton *)sender {

    if (self.height.constant == 200) {

       self.height.constant = 88;
    }else
       self.height.constant = 200;

    [self.view setNeedsUpdateConstraints];

    [UIView animateWithDuration:0.5f animations:^{
        [self.view layoutIfNeeded];
    }];

    DRect(_scrollView.frame)
    DRect(self.view.frame)
    DRect(sender.frame)
    NSLog(sender.selected ? @"Selected" : @"Not Selected");

}

Drect 是帧的 nslog。所以控制台输出是:
2013-04-08 17:04:19.264 TouchSelectApp[93618:c07] CGRect ( 20.000000, -112.000000, 62.000000, 200.000000)
2013-04-08 17:04:19.266 TouchSelectApp[93618:c07] CGRect ( 0.000000, 300.000000, 320.000000, 88.000000)
2013-04-08 17:04:19.266 TouchSelectApp[93618:c07] CGRect ( 7.000000, 78.500000, 48.000000, 44.000000)

(lldb) po [[UIApp keyWindow] recursiveDescription] 的输出
我必须添加一个额外的按钮来创建调试停止。我关心的按钮在 SelectableTag 内部。
$0 = 0x0887e0e0 <UIWindow: 0x7197a90; frame = (0 0; 320 568); autoresize = W+H; layer =    <UIWindowLayer: 0x7199620>>
   | <UIView: 0x719ed20; frame = (0 20; 320 548); autoresize = W+H; layer = <CALayer: 0x719edd0>>
   |    | <UIRoundedRectButton: 0x719b9d0; frame = (51 464; 73 44); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x719baf0>>
   |    |    | <UIGroupTableViewCellBackground: 0x719c2a0; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x719c370>>
   |    |    | <UIImageView: 0x719da50; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x719dd20>>
   |    |    | <UIButtonLabel: 0x719d0f0; frame = (12 12; 49 19); text = 'Button'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x719d1e0>>
   |    | <UIView: 0x71a1160; frame = (0 300; 320 88); autoresize = RM+BM; layer = <CALayer: 0x71a11c0>>
   |    |    | <SelectableTag: 0x719fcc0; baseClass = UIScrollView; frame = (20 -112; 62 200); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x71a04e0>; layer = <CALayer: 0x719fef0>; contentOffset: {0, 0}>
   |    |    |    | <UIImageView: 0x71a0c20; frame = (55 193; 7 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x71a0cc0>>
   |    |    |    | <UIImageView: 0x71a0dd0; frame = (55 62; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x71a0e70>>
   |    |    |    | <UIRoundedRectButton: 0x719ddd0; frame = (-1 7; 64 44); opaque = NO; layer = <CALayer: 0x719b630>>
   |    |    |    |    | <UIGroupTableViewCellBackground: 0x719d630; frame = (0 0; 64 44); userInteractionEnabled = NO; layer = <CALayer: 0x719b690>>
   |    |    |    |    | <UIImageView: 0x719dea0; frame = (1 1; 62 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x719d990>>
   |    |    |    |    | <UIButtonLabel: 0x71a1630; frame = (12 12; 40 19); text = 'Test1'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x71a1e50>>

(lldb)

最佳答案

来自 the documentation of hitTest:withEvent: in the UIView Class Reference :

Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.



ScrollView 的父级是地址为 0x71a1160 的 View 。 .请注意, ScrollView 的 frame.origin.y是-112。因此, ScrollView 的前 112 个点超出了其父级的范围。 ScrollView 的那部分将永远不会收到触摸。您的 Test1 按钮完全位于 ScrollView 的那部分。

关于ios - 更改容器高度后 UIButton 不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889093/

相关文章:

iphone - iOS 从经过身份验证的 URL 流式传输音频

ios - Xcode "Analyze"消息混淆

objective-c - 迭代 NSMutableArray 无法正常工作

objective-c - 从另一个 ViewController 设置 UIScrollView 的位置

ios - ENABLE_BITCODE 在 xcode 7 中做了什么?

ios - React-native IOS 应用程序未在模拟器中显示 Assets

html - 单击 UIWebView 时如何在 Safari 中打开链接?

iphone - UISearchBar 搜索图标在 iOS 7 中未左对齐

iphone - 应用程序不在 ios 7 的后台运行

ios - 类理解: Unexpected non-void return value in void function (swift 3)