Iphone UIButton not working in nested UIViews

前端 未结 6 2028
醉梦人生
醉梦人生 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:02

    Does the new view accept user interaction? In other words, is userInteractionEnabled enabled on the view "Characters"?

    0 讨论(0)
  • 2021-01-17 15:04

    FINALLY!!!!!!

    I had to init all the views with initWithFrame and pass in valid frame rects. Init should be used with controllers and initWithFrame passing rects for UIViews!!!!

    characterView = [ [ CharacterView alloc ] initWithFrame:CGRectMake(0, 0, 480, 320)]; 
    then 
    characterMale = [ [ CharacterMale alloc ] initWithFrame:frame];
    
    0 讨论(0)
  • 2021-01-17 15:05

    Another thing to consider is that UIButton subviews that have been added a UIImageView don't work at all. You need to create a UIView that you add both the image view and the buttons to.

    This is probably because interaction is turned off by default on image views, but I've not checked this.

    0 讨论(0)
  • 2021-01-17 15:05

    I have had luck with a selector for a UIButton created in drawRect by using UIControlEventTouchDown instead of the popular UIControlEventTouchUpInside.

    0 讨论(0)
  • For me the issue was caused because, the origin of the button frame was bigger then the parent frame.

    0 讨论(0)
  • 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];
      }
    }
    
    0 讨论(0)
提交回复
热议问题