iphone将通知图标添加到uitoolbar

时间:2013-09-20 18:26:41

标签: iphone ios xcode4 uitoolbar

我正在构建一个每5分钟从服务器获取新数据的应用程序。我想通知用户添加到以前的数据的新数据。我正在使用工具栏,我想在特定的地方显示此图标。看到我使用的代码:

// notification button
UIButton *notifications=[UIButton buttonWithType:UIButtonTypeRoundedRect];
notifications.bounds = CGRectMake( 40, 34, 20,20 );
[notifications setTitle:@"1" forState:UIControlStateNormal];
notifications.backgroundColor = [ UIColor clearColor];
[notifications addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];


// add to uibutton
UIBarButtonItem *notify = [[UIBarButtonItem alloc] initWithCustomView:notifications];

items = [NSArray arrayWithObjects: add, search,flexItem,currentLoc,notify,logoLabel, nil];

但是按钮没有显示在声明的地方(40,30),有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您需要将UIButton传递为:

// notification button
UIButton *notifications=[UIButton buttonWithType:UIButtonTypeRoundedRect];
notifications.bounds = CGRectMake( 40, 34, 20,20 );
[notifications setTitle:@"1" forState:UIControlStateNormal];
notifications.backgroundColor = [ UIColor clearColor];
[notifications addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

// Pass your button in UIBarButtonItem here 
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:notifications];
[toolBar setItems:[NSArray arrayWithObject:barButton]];

希望它对你有所帮助。