【发布时间】:2023-03-03 01:38:01
【问题描述】:
我有以下代码来自定义 UIBarButtonItem (locationButton) 的显示:
UIButton *locationButtonAux = [UIButton buttonWithType:UIButtonTypeCustom];
[locationButtonAux setFrame:CGRectMake(0, 0, LOCATION_BUTTON_WIDTH, LOCATION_BUTTON_HEIGHT)];
[locationButtonAux setBackgroundImage:[UIImage imageNamed:@"location_button.png"] forState:UIControlStateNormal];
[locationButtonAux setBackgroundImage:[UIImage imageNamed:@"location_button.png"] forState:UIControlStateHighlighted];
[locationButtonAux addTarget:self action:@selector(userLocation) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:locationButtonAux];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"title"
style:UIBarButtonItemStyleDone
target:nil action:@selector(someMssage)];
[locationButton setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = locationButton;
我想把这个按钮的位置调整到 NavigationBar 中,因为我的 NavigationBar 比正常高(使用 Appeareance 自定义)。
我正在使用 setBackgroundVerticalPositionAdjustment 方法(如您在代码中所见)来执行此操作。但是我的 UIBarButtonItem 根本不工作,无论我放什么偏移,按钮总是出现在栏的底部附近(快照)。 但是,如果我使用具有普通样式的普通 UIBarButtonItem(我称之为按钮的那个),我可以看到按钮的位置将如何改变 -20 的偏移量。很奇怪...有什么想法吗?
谢谢!
【问题讨论】:
-
看看stackoverflow.com/a/16442006/1328096,它会为您提供充分的帮助..
-
一个从不接受答案的提问者(复选标记)不会为人们花时间帮助他提供太多动力。
标签: iphone objective-c uibarbuttonitem uiappearance