UINavigationController中的UIButton接收来自错误位置的水龙头

时间:2012-02-25 02:29:38

标签: ios uinavigationcontroller uibutton

我正在尝试以编程方式创建一个UIViewController子类,其视图包含一些按钮,然后将其添加到UINavigationController。当我这样做时,你必须触摸每个按钮上方大约40或50个点才能触发它(触摸按钮本身什么都不做)。按钮和您必须触摸的实际区域之间的距离似乎与UINavigationController的导航栏的高度相匹配,因此我怀疑它与它有关。对于如何解决这个问题,有任何的建议吗?这是创建窗口和按钮的代码(来自我的UIViewController子类):

- (void)loadView
{
    ...

    UIWindow* mainView = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    [mainView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [mainView makeKeyAndVisible];

    [self setView:mainView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton* encryptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [encryptButton setTitle:@"Encrypt" forState:UIControlStateNormal];
    [encryptButton setFrame:CGRectMake(([[self view] bounds].size.width - 150.0f)/2.0f,
                                           ([[self view] bounds].size.height - 120.0f)/2.0f, 150.0f, 40.0f)];
    [encryptButton setBounds:CGRectMake(0.0f, 0.0f, 150.0f, 40.0f)];
    [encryptButton addTarget:self action:@selector(encryptPressed:) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:encryptButton];
    [encryptButton release];

    UIButton* decryptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [decryptButton setTitle:@"Decrypt" forState:UIControlStateNormal];
    [decryptButton setFrame:CGRectMake(([[self view] bounds].size.width - 150.0f)/2.0f,
                                           ([[self view] bounds].size.height - 120.0f)/2.0f + 80.0f, 150.0f, 40.0f)];
    [decryptButton addTarget:self action:@selector(decryptPressed:) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:decryptButton];
    [decryptButton release];
}

以下是设置UINavigationController的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    UUMainWindowViewController* mainWinController = [[UUMainWindowViewController alloc] init];

    UINavigationController* mainNavController = [[UINavigationController alloc]
                                                     initWithRootViewController:mainWinController];
    [mainNavController navigationBar].barStyle = UIBarStyleBlack;
    [self.window setRootViewController:mainNavController];

    [self.window makeKeyAndVisible];
    return YES;
}

1 个答案:

答案 0 :(得分:0)

找出我自己的答案。事实证明这是一个新手错误,我在UIViewController中构建一个UIWindow,而我真的需要构建一个UIView。显然应该只有一个UIWindow,所以大概这不起作用,因为UIWindow假设它有自己的整个屏幕空间。