我将自定义UIBarButton
项添加到UINavigationBar
作为rightbarbuttonitems
。当设备转到UIBarButton
时,我在landscape mode
中再添加两个项目。我正在调用rotation
,我正在根据设备方向正确地从UIBarbuttons
数组中删除项目,但我的导航栏项目集永远不会得到更新。
如果我从肖像模式开始,它会显示它所设想的内容。当我旋转到landscape
时,新项目未添加到navigation bar
,反之亦然,当设备转到纵向时,额外项目将不会进入。
我不确定如何在设备旋转时更改navigationbaritems。对我而言,我正在做所有事情。
-(void)setUpOnRotation:(BOOL)toPortrait{
_searchMessage = [[UILabel alloc] initWithFrame:CGRectMake(3.0, 0, 110, 20)];
_searchMessage.numberOfLines = 2;
_searchMessage.textColor = [UIColor blackColor];
_searchMessage.font = [UIFont systemFontOfSize:8.0];
_searchMessage.text = @"";
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 15, 130, 25)];
_searchBar.delegate = self;
_searchBar.showsCancelButton = NO;
UIBarButtonItem* doneButton = [BarButtons doneButtonWithTarget:self action:@selector(cancel)];
UIBarButtonItem *searchActivityItem = [[UIBarButtonItem alloc] initWithCustomView:_searchMessage];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:_searchBar];
UIBarButtonItem *in = [BarButtons nativeButtonNamed:@"In"
title:@"In"
target:self
action:@selector(In)];
UIBarButtonItem *out = [BarButtons nativeButtonNamed:@"Out"
title:@"Out"
target:self
action:@selector(Out)];
NSMutableArray *itemsSet;
itemsSet = [NSMutableArray arrayWithObjects:in,
out,
searchItem,
searchActivityItem,
doneButton, nil];
if (toPortrait) {
if ([itemsSet containsObject:searchItem] || [itemsSet containsObject:searchActivityItem]) {
[itemsSet removeObject:searchActivityItem];
[itemsSet removeObject:searchItem];
}
}
[_searchBar release];
[searchItem release];
[_searchActivity release];
[_searchMessage release];
[searchActivityView release];
self.navigationItem.rightBarButtonItems = itemsSet;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self setUpOnRotation:UIInterfaceOrientationIsPortrait(toInterfaceOrientation)];
} -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
[self setUpOnRotation:UIInterfaceOrientationIsPortrait(self.interfaceOrientation)];}
注意:setUpOnRotaion
方法是从willAnimateRotationToInterfaceOrientation
和viewWillAppear
调用UIInterfaceOrientationIsPortrait(self.intefaceOrientation)
,它确实给了我正确的结果。我已经调试了。
答案 0 :(得分:1)
在willAnimateRotationToInterfaceOrientation中更改方向检查
if([uidevice orientaiton]statusbarorientation]){
self.naviogationite.rightbarbuttonitems = @[btn1,btn2,btn3];
}
else{
self.naviogationite.rightbarbuttonitems = @[abtn1,abtn2,abnt3];
}
u can do like this
can u do like this
NSMutableArray *itemsSet;
if(!toPortrait){
itemsSet = [NSMutableArray arrayWithObjects:in,
out,
searchItem,
searchActivityItem,
doneButton, nil];
}
if (toPortrait) {
itemsSet = [NSMutableArray arrayWithObjects:in,
out,
doneButton, nil];
}