網頁

2012年4月24日 星期二

UIToolBar UIBarButtonItem

如何在UIToolBar上使用UIBarButtonItem定制自己的工具条
 
- (void) initToolbars: (BOOL)aPlaying
{
    CGFloat width =   self.view.frame.size.width;
    CGFloat height =  self.view.frame.size.height;
    UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    
    toolBar.barStyle = UIBarButtonItemStyleBordered;
    [toolBar sizeToFit];
    
    toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;//这句作用是切换时宽度自适应.
    
    UIBarButtonItem* barItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:@selector(buttonPress:)];
    
    [toolBar setItems:[NSArray arrayWithObject:barItem]];
    
    [self.view addSubview:toolBar];

}
-(void)buttonPress:(id)sender
{
    NSLog(@"ToolBar Button taped.");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initToolbars: NO];
}

IOS 3.0以后,navigation controller自带toolbar


在toolbar中加入Segmented Control 並置中



    [self.navigationController setToolbarHidden:NO animated:NO]; 
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    self.navigationController.toolbar.barStyle = UIBarStyleBlack; 
    
    [space setWidth:44.5];
    
    
    [self setToolbarItems: [NSArray arrayWithObjects: space, sgBarButton, nil] animated: NO];

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。