简体   繁体   中英

This code only works on iPhone Retina 4-inch simulator

I'm developing an iOS 5+ application with latest SDK and I have a custom UIView with a custom XIB .

This is TopMenuView.h :

#import <UIKit/UIKit.h>

@interface TopMenuView : UIView

@property (weak, nonatomic) IBOutlet UIButton *MenuButton;
@property (weak, nonatomic) IBOutlet UILabel *MenuLabel;
@property (weak, nonatomic) IBOutlet UIButton *SearchButton;
@property (weak, nonatomic) IBOutlet UIButton *ProfileButton;
@property (weak, nonatomic) IBOutlet UIButton *HomeButton;

@property (weak, nonatomic) UIViewController* parentController;

- (IBAction)MenuClicked:(id)sender;
- (IBAction)SearchClicked:(id)sender;
- (IBAction)ProfileClicked:(id)sender;
- (IBAction)HomeClicked:(id)sender;

+ (id)topMenuView;

@end

This is TopMenuView.m :

#import "TopMenuView.h"
#import "SearchViewController.h"
#import "ProfileViewController.h"
#import "HomeViewController.h"

#import "SWRevealViewController.h"

@implementation TopMenuView

@synthesize parentController;

+ (id)topMenuView
{
    TopMenuView* topView = [[[NSBundle mainBundle] loadNibNamed:@"TopMenuView"
                                                          owner:nil
                                                        options:nil] lastObject];
    // make sure customView is not nil or the wrong class!
    if ([topView isKindOfClass:[TopMenuView class]])
    {
        [topView setFrame:CGRectMake(0, 0, 320, 49)];
        return topView;
    }
    else
        return nil;
}

#pragma mark -
#pragma mark IBAction methods

- (IBAction)MenuClicked:(id)sender
{
    [self.parentController.revealViewController revealToggle:sender];
}

- (IBAction)SearchClicked:(id)sender
{
    SearchViewController* search =
        [[SearchViewController alloc] initWithNibName:@"SearchViewController"
                                               bundle:nil];

    [self.parentController.revealViewController setFrontViewController:search];
}

- (IBAction)ProfileClicked:(id)sender
{
    ProfileViewController* profile =
        [[ProfileViewController alloc] initWithNibName:@"MyProfileViewController"
                                                bundle:nil];

    [self.parentController.revealViewController setFrontViewController:profile];
}

- (IBAction)HomeClicked:(id)sender
{
    HomeViewController* home =
        [[HomeViewController alloc] initWithNibName:@"HomeViewController"
                                             bundle:nil];

    [self.parentController.revealViewController setFrontViewController:home];
}

@end

And on UIViewController I do this to show it:

- (void)viewDidLoad
{
    [super viewDidLoad];

    topMenuView = [TopMenuView topMenuView];
    topMenuView.MenuLabel.text = @"Home";
    topMenuView.parentController = self;
    [self.view addSubview:topMenuView];

    // Set the gesture to open the slide menu.
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

And this is the view:

在此处输入图片说明

This code works perfectly only on iPhone Retina 4-inch simulator . On iPhone Retina 3.5-inch and on iPhone simulator, it doesn't work.

The problem is that I do a click over any of that "buttons" it do nothing. No Touch Inside Up event is throw (I set a debug point inside the IBAction method).

I haven't test it on a real iPhone because I don't have a developer license yet.

What's happening? Why it doesn't work on iPhone 3.5-inch?

You probably have another view which covers over your buttons. Check your xib file for a view that is possibly being expanded by the system when it has a different size screen.

I had the same problem. Everything worked fine, until I uncheck "Use AutoLayout" button in Storyboard. I have UIView with button placed on it. And have action related to this button. Then in iPhone Retina 3.5-inch Simulator it doesn't respond to that action. Finally, I found the solution in the Storyboard. It sets the height of my view where button is placed to 0. I don't know why. So I just specify its height programmatically. Hope this will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM