2

我已经应用了一个代码来向 uibarbuttonitem 添加一个 uibutton,问题是按钮超出了框架

代码是

        a1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [a1 setFrame:CGRectMake(0.0f, 0.0f, 65.0f, 63.0f)];
    [a1 addTarget:self.slideMenuController action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
    [a1 setImage:[UIImage imageNamed:@"menu-enable-bt.png"] forState:UIControlStateNormal];

    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 65.0,63.0) ];
    [view addSubview:a1];

    menuButton = [[UIBarButtonItem alloc] initWithCustomView:view];


    rootViewController.navigationItem.leftBarButtonItem = menuButton;

我不知道怎么了在此处输入图像描述

4

4 回答 4

0

尝试这个..

 a1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [a1 setFrame:CGRectMake(0.0f, 0.0f, 30.0f, 30.0f)];
    [a1 addTarget:self.slideMenuController action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
    [a1 setImage:[UIImage imageNamed:@"menu-enable-bt.png"] forState:UIControlStateNormal];

    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 35.0,35.0) ];
    [view addSubview:a1];

    menuButton = [[UIBarButtonItem alloc] initWithCustomView:view];


    rootViewController.navigationItem.leftBarButtonItem = menuButton;
于 2013-06-17T13:05:38.040 回答
0

UINavigationBar高度为 44,您设置框架 (0.0, 0.0, 65.0,63.0),更改框架高度以适应UINavigationBar

于 2013-06-17T13:07:22.120 回答
0

你试过降低高度CGRectMake(0.0, 0.0, 65.0,63.0)吗?是的,你很可能会泄露view and menuButton

也代替 -

a1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [a1 setFrame:CGRectMake(0.0f, 0.0f, 65.0f, 63.0f)];
    [a1 addTarget:self.slideMenuController action:@selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
    [a1 setImage:[UIImage imageNamed:@"menu-enable-bt.png"] forState:UIControlStateNormal];

    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 65.0,63.0) ];
    [view addSubview:a1];

    menuButton = [[UIBarButtonItem alloc] initWithCustomView:view];

你可以给menuButton = [[UIBarButtonItem alloc] initWithCustomView:a1];然后-

rootViewController.navigationItem.leftBarButtonItem = menuButton;

[menuButton release];
于 2013-06-17T13:08:37.703 回答
0

试试这样...

UIButton * a1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [a1 setFrame:CGRectMake(0.0f, 0.0f, 65.0f, 33.0f)];
    [a1 addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
    [a1 setImage:[UIImage imageNamed:@"sc.png"] forState:UIControlStateNormal];


   UIBarButtonItem* menuButton = [[UIBarButtonItem alloc] initWithCustomView:a1];


    self.navigationItem.leftBarButtonItem = menuButton;

样品参考这里

于 2013-06-17T13:13:50.663 回答