Iphone UIButton not working in nested UIViews

前端 未结 6 2029
醉梦人生
醉梦人生 2021-01-17 14:43

This is so damn simple im sure! Im missing something and im exhausted from trying to fix it. hopefully someone can help.

The Button in CharacterView.m works but the

6条回答
  •  礼貌的吻别
    2021-01-17 15:21

    I had a similar issue when tried to add the button during the initialization of an UIView with a frame of CGRectZero:

    @implementation SomeView
    ...
    - (id)initWithTitleImage:(UIImage *) newTitleImage {
      // BROKEN: frame with CGRectZero.
      self = [super initWithFrame:CGRectZero];
      if (self) {
        UIButton *someButton = ...;
        [self addSubview someButton];
      }
    }
    

    Once I changed the frame to a proper rectangle, the button worked:

    @implementation SomeView
    ...
    - (id)initWithTitleImage:(UIImage *) newTitleImage {
      // WORKING: frame with proper CGRect.
      self = [super initWithFrame:CGRectMake(0, 0, 480, 320)];
      if (self) {
        UIButton *someButton = ...;
        [self addSubview someButton];
      }
    }
    

提交回复
热议问题