Note that there are some explanatory texts on larger screens.

plurals
  1. POadding a button in an UIView
    primarykey
    data
    text
    <p>I'm trying to add a button to a UIview, to cover the whole area of the view.. like this:</p> <pre><code>-(MBNHomeScreenButton*) initWithImage:(UIImage*)image andHighlightedImage:(UIImage*)highlightedImage andTitle:(NSString*)aTitle withSelector:(SEL)actionButton forDelegate:(UIViewController*)viewController{ self = [super init]; if (self) { self.frame = CGRectMake(0, 0, 100, 120); self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage]; self.imageView.center = self.center; self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT); self.imageView.contentMode = UIViewContentModeScaleAspectFit; self.imageView.highlighted = NO; [self addSubview:self.imageView]; self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)]; self.title.text = aTitle; self.title.textAlignment = UITextAlignmentCenter; self.title.textColor = [UIColor whiteColor]; self.title.backgroundColor = [UIColor clearColor]; [self addSubview:self.title]; // This creates a transparent button over the view UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //button.backgroundColor = [UIColor redColor]; [button setFrame:self.frame]; [button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside]; [self addSubview: button]; } return self;} </code></pre> <p>In the ViewController I create the View like this:</p> <pre><code>m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE] andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE] andTitle:@"News" withSelector:@selector(newsButtonPressed) forDelegate:self]; m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4); [backgroundImgView addSubview:m_newsHSButton]; </code></pre> <p>Still... the newsButtonPressed doesn't get called.</p> <p>Can you help? Thanks!</p> <p>==================== [edit] Ok... figured it out.. Thanks guys. It was the fact that I was trying to add the view as a subview to a UIImageView with the last line:</p> <pre><code>[backgroundImgView addSubview:m_newsHSButton]; </code></pre> <p>Apparently, it won't get touches anymore.</p> <p>Thanks for the effort guys!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload