【iOS】UIToolbar


 

  • 初始化

    IBOutlet UIToolbar    *toolbar;

    //create toolbar using new
    toolbar = [UIToolbar new];
    toolbar.barStyle = UIBarStyleDefault;
    [toolbar sizeToFit];
    toolbar.frame = CGRectMake(0, 410, 320, 50);

    //Add buttons
    UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                 target:self
                                                                                 action:@selector(pressButton1:)];

    UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                                 target:self
                                                                                 action:@selector(pressButton2:)];

    UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                                                                 target:self
                                                                                 action:@selector(pressButton3:)];

    //Use this to put space in between your toolbox buttons
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                              target:nil
                                                                              action:nil];

    //Add buttons to the array
    NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];

    //release buttons
    [systemItem1 release];
    [systemItem2 release];
    [systemItem3 release];
    [flexItem release];

    //add array of buttons to toolbar
    [toolbar setItems:items animated:NO];

    [self.view addSubview:toolbar];

  • style

    typedef enum {
        UIBarButtonSystemItemDone,
        UIBarButtonSystemItemCancel,
        UIBarButtonSystemItemEdit, 
        UIBarButtonSystemItemSave, 
        UIBarButtonSystemItemAdd,
        UIBarButtonSystemItemFlexibleSpace,
        UIBarButtonSystemItemFixedSpace,
        UIBarButtonSystemItemCompose,
        UIBarButtonSystemItemReply,
        UIBarButtonSystemItemAction,
        UIBarButtonSystemItemOrganize,
        UIBarButtonSystemItemBookmarks,
        UIBarButtonSystemItemSearch,
        UIBarButtonSystemItemRefresh,
        UIBarButtonSystemItemStop,
        UIBarButtonSystemItemCamera,
        UIBarButtonSystemItemTrash,
        UIBarButtonSystemItemPlay,
        UIBarButtonSystemItemPause,
        UIBarButtonSystemItemRewind,
        UIBarButtonSystemItemFastForward,
    #if __IPHONE_3_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
        UIBarButtonSystemItemUndo,
        UIBarButtonSystemItemRedo,
    #endif
    #if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
        UIBarButtonSystemItemPageCurl,
    #endif
    } UIBarButtonSystemItem;

  • Type 初始化, 提供 Custom/RoundedRect/Detail/Info/ContactAdd, 使用 Custom 時記得要給 Frame

    UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button3 addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [button3 setTitle:@"+" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button3.titleLabel.font = [UIFont systemFontOfSize:25];
    button3.frame = CGRectMake(100, 170, 100, 30);

    // create button item -- possible because UIButton subclasses UIView!
    UIBarButtonItem *systemItem3=[[UIBarButtonItem alloc] initWithCustomView:button3];

  • type

    typedef enum {
        UIButtonTypeCustom = 0,
        UIButtonTypeRoundedRect,
        UIButtonTypeDetailDisclosure,
        UIButtonTypeInfoLight,
        UIButtonTypeInfoDark,
        UIButtonTypeContactAdd,
    } UIButtonType;

 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com