UIVut中的UIButton作为子视图而不是攻丝(无动作,无颜色变化)

0 iphone objective-c subview uibutton uiview

我有一个UIView,它是UINavigationController Ex 的子视图.

- (void) ViewDidload{
    UIView *theSubView = [[UIView alloc] init];

    UIButton *button = [UIButton . . . .]
    . . . .
    [theSubView addSubView:button];
    [self.view addSubview:theSubView];
}
Run Code Online (Sandbox Code Playgroud)

我放在"theSubView"里面的所有标签和按钮都显示出来,但它们对任何触摸都没有反应.

UIButton还不是"theSubView"的子视图,如果它的工作原理,但是当它是自我的子视图它的工作原理.

所以我的问题是如何在"TheSubView"(UIView)工作中制作Button?它没有点亮或任何东西.

Joh*_*mpe 10

如果我是正确的那么你问题实际上是你使用

[[UIView alloc] init]
Run Code Online (Sandbox Code Playgroud)

而不是指定的初始化程序

initWithFrame:(CGRect)frame
Run Code Online (Sandbox Code Playgroud)

您正在创建一个边界为0,0的视图.您应该使用指定的初始化程序创建它并执行类似的操作

/*
 * note that the values are just sample values.
 * the view's origin is  0,0 and it's width 320, it's height 480
*/
UIView *theSubView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480];
Run Code Online (Sandbox Code Playgroud)

如果你设置

theSubView.clipsToBounds = YES
Run Code Online (Sandbox Code Playgroud)

结果应该是你根本看不到你的按钮,因为视图的大小是0,0.

UIView只能响应自己界限内的触摸,这就是为什么你的按钮不响应任何触摸的原因.