0

我想在我的应用程序中创建一个静态页脚。我使用了 UIToolbar。现在我想添加一些带有自定义照片的按钮。工具栏工作正常,但不显示按钮。

这是我用来添加按钮的代码:

UIImage *facebookImage=[[UIImage alloc]initWithContentsOfFile:@"facebook.png"];
UIImage *twitterImage=[[UIImage alloc]initWithContentsOfFile:@"twitter.png"];
UIImage *messageImage=[[UIImage alloc]initWithContentsOfFile:@"message.png"];
UIImage *emailImage=[[UIImage alloc]initWithContentsOfFile:@"email.png"];


UIBarButtonItem *facebook=[[UIBarButtonItem alloc]initWithImage:facebookImage style:UIBarButtonItemStyleBordered target:Nil action:Nil];
UIBarButtonItem *twitter=[[UIBarButtonItem alloc]initWithImage:twitterImage style:UIBarButtonItemStylePlain target:Nil action:Nil];
UIBarButtonItem *message=[[UIBarButtonItem alloc]initWithImage:messageImage style:UIBarButtonItemStylePlain target:Nil action:Nil];
UIBarButtonItem *email=[[UIBarButtonItem alloc]initWithImage:emailImage style:UIBarButtonItemStylePlain target:Nil action:Nil];

NSArray *toolbarButtons = [NSArray arrayWithObjects: facebook, twitter,email,message, nil];
[Footer setItems:toolbarButtons];
4

2 回答 2

2

在您的代码更改中:

UIImage *facebookImage=[[UIImage alloc]initWithContentsOfFile:@"facebook.png"];
UIImage *twitterImage=[[UIImage alloc]initWithContentsOfFile:@"twitter.png"];
UIImage *messageImage=[[UIImage alloc]initWithContentsOfFile:@"message.png"];
UIImage *emailImage=[[UIImage alloc]initWithContentsOfFile:@"email.png"];

和 :

UIImage *facebookImage= [UIImage imageNamed:@"facebook.png"];
UIImage *twitterImage= [UIImage imageNamed:@"twitter.png"];
UIImage *messageImage= [UIImage imageNamed:@"message.png"];
UIImage *emailImage= [UIImage imageNamed:@"email.png"];

如果你想使用- (id)initWithContentsOfFile:(NSString *)path方法,你应该得到文件的完整路径名:

NSString* Path = [[NSBundle mainBundle] pathForResource:@"your_file_name" 
                                            ofType:@"the_file_extension"]; 
于 2013-10-19T13:25:13.590 回答
0

在以下代码中,我 UIBarButtonflexSpace添加了两个..
您可以根据需要添加 UIBarButton

UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    Toolbar = UIBarStyleBlackTranslucent;
    [Toolbar sizeToFit];

     NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
    [barItems addObject:btnCancel];

    UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
    [barItems addObject:btnDone];

    [Toolbar setItems:barItems animated:NO];

点击栏按钮时调用以下方法

-(void)Cancel
{
  // Write Code for Cancel Method
}

-(void)done
{
  // Write Code for Done Method
}

按照我的回答并UIBarButtonItem根据您的要求添加,可能此代码对您有帮助。

于 2013-10-19T13:00:07.530 回答