如何以编程方式制作UIToolbar?

时间:2012-07-21 02:45:44

标签: iphone ios xcode

我的大多数相机覆盖屏幕都覆盖着不透明的覆盖层。对于剩下的部分,我想添加一个带有几个按钮的UIToolbar。请注意如何以编程方式点击这些按钮!

以下是我添加叠加层的方式,看起来很完美。

- (UIView*)CommomOverlay  {
    //Both of the 428 values stretch the overlay bottom to top for overlay function. It doesn't cover it. 
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,428)];
    UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,428)];
    [FrameImg setImage:[UIImage imageNamed:@"newGraphicOverlay.png"]];
    [view addSubview:FrameImg];
    return view; 
}

同样,我如何添加一个工作工具栏(可点击并完全可以使用2个按钮)?

2 个答案:

答案 0 :(得分:2)

试试这个:(在你的viewDidLoad或viewWillAppear中)

//create toolbar and set origin and dimensions
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 428, 320, 32)];

//create buttons and set their corresponding selectors
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(button1Tap:)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(button2Tap:)];

//if you want custom icons, use something like this:
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon1_portrait.png"] landscapeImagePhone:[UIImage imageNamed:@"icon1_landscape.png"] style:UIBarButtonItemStylePlain target:self action:@selector(button3Tap:)];

//add buttons to the toolbar
[toolbar setItems:[NSArray arrayWithObjects:button1, button2, nil]];

//add toolbar to the main view
[self.view addSubview:toolbar];
  • 创建这样的选择器:

    -(void)button1Tap:(id)sender {
        NSLog(@"Button 1 was tapped!");
        // do whatever needs to happen
    }
    

答案 1 :(得分:0)

如果我理解正确,您只需在函数中添加UIToolBar

- (UIView*)CommomOverlay  {
    //Both of the 428 values stretch the overlay bottom to top for overlay function. It doesn't cover it.
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,428)];
    UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,428)];
    [FrameImg setImage:[UIImage imageNamed:@"newGraphicOverlay.png"]];
    UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(myFunction)];
    [myToolBar setItems:[NSArray arrayWithObjects:myBarButtonItem, nil] animated:YES];
    [FrameImg addSubview:myToolBar];
    [view addSubview:FrameImg];
    return view;
}