2

我已经以编程方式将按钮添加到我的视图中,但是当我在模拟器上进行测试时,视图上只有工具栏可见。

我究竟做错了什么?

我在头文件中定义了我的 ui 元素,并在主文件中合成,然后在 viewDidLoad 方法中初始化。但是有一点不对劲。

我的代码如下所示;

#import "ViewController.h"

@implementation ViewController


@synthesize iAmLost, iAmLooking, about, howToUse,appsFromKodAtolye, exit;


- (void)viewDidLoad
{
[super viewDidLoad];

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];

iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 200.0, 10.0)]; 

iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 200.0, 10.0) ];

[iAmLooking addTarget:self 
             action:@selector(iAmLookingScreen) 
   forControlEvents:UIControlEventTouchUpInside];


about = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];

howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];

exit = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStyleBordered  target:self action:@selector(exitApp:)];

NSArray *buttons = [[NSArray alloc] 
                    initWithObjects:exit, nil];

toolbar.items = buttons;


[self.view addSubview:iAmLost];
[self.view addSubview:iAmLooking];
[self.view addSubview:about];
[self.view addSubview:toolbar];



}
4

4 回答 4

3

您将按钮放在工具栏下方,将每个项目添加到 0,0。由于工具栏是您添加到子视图的最后一项,它将出现在所有按钮的顶部,因此您将无法看到这些按钮。

框架由4部分组成x坐标,y坐标,高度,宽度

尝试更改这些的 x 和 y 坐标

例如

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];

iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,100, 200.0, 10.0)]; 

iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 200, 200.0, 10.0) ];

您还应该注意,您目前还没有为按钮或任何标题设置任何样式。设置标题或背景颜色可能是值得的,这样您就可以真正看到您在屏幕上放置了哪些按钮并分辨出哪个是哪个。

于 2013-06-26T10:36:11.187 回答
2

由于您将工具栏添加为最后一个子视图,因此所有按钮都位于工具栏下方。您必须将按钮添加为工具栏的子视图。另请注意,您将所有按钮定位在同一位置。

于 2013-06-26T10:37:27.487 回答
1

您总共有四个按钮和一个工具栏。工具栏的框架是完全正确的,即

       (0, 0, 320.0, 40.0) 

但是对于按钮,您为所有人提供相同的坐标,因此它是重叠的。更好的是,您将更改按钮坐标。如果您想在工具栏上添加这些按钮,请正确设置坐标并使用

     [toolBar addSubView: button];
于 2013-06-26T10:48:45.520 回答
0

Simple, just check the origin x & y for each Button

UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];

    UIButton *iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(5,50, 200.0, 10.0)];
    iAmLost.backgroundColor = [UIColor redColor];
    UIButton * iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 70, 200.0, 10.0) ];

    [iAmLooking addTarget:self
                   action:@selector(iAmLookingScreen)
         forControlEvents:UIControlEventTouchUpInside];


    UIButton * about = [[UIButton alloc]initWithFrame:CGRectMake(0, 90, 200.0, 10.0)];
    about.backgroundColor = [UIColor greenColor];

   UIButton *  howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 120, 200.0, 10.0)];
    howToUse.backgroundColor = [UIColor blackColor];

  UIBarButtonItem*  exit = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStyleBordered  target:self action:@selector(exitApp:)];

    NSArray *buttons = [[NSArray alloc]
                        initWithObjects:exit, nil];

    toolbar.items = buttons;


    [self.view addSubview:iAmLost];
    [self.view addSubview:iAmLooking];
    [self.view addSubview:howToUse];
    [self.view addSubview:about];
    [self.view addSubview:toolbar];
于 2013-06-26T11:20:41.337 回答