ios:如何获取导航栏中rightBarButtonItem对应self.view的坐标?
PHP中文网
PHP中文网 2017-04-18 09:05:01
0
3
1017

1.我想做一个动画,需要知道rightBarButtonItem对应self.view中的位置,即:

CGPoint toPoint = [self.navigationController.navigationBar convertPoint:self.navigationItem.titleView.center toView:self.view];

但是获取到的位置坐标始终不正确:

2.问题是:
怎么能够获取到导航栏中的各个navigationItem对应在self.view中的位置呢?

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
迷茫

self.view is a view placed in the navigation controller, and the navigation bar is the same, so they are equal. It does not mean that the navigation bar is a subview of self.view. Naturally, there is no way to calculate the navigation bar. The position of the sub-items in self.view.

I think what you want to calculate is the position of the item in the window or navigationController.view, right?

You can use view debugging to see the hierarchical relationship between various views.

PHPzhong
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[btn setTitle:@"right" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

CGRect frameInNaviView = [self.navigationController.view convertRect:btn.frame fromView:btn.superview];

UIView *vShow = [[UIView alloc] initWithFrame:frameInNaviView];
vShow.backgroundColor = [UIColor redColor];
[self.navigationController.view addSubview:vShow];

frameInNaviView is what you need

迷茫

Found through testing:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[btn setTitle:@"right" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

CGRect frameInNaviView = [self.navigationController.view convertRect:btn.frame fromView:btn.superview];

This can indeed get the correct position; but after testing on the iPad, it was found that the same code got the result {{0, 0}, {44, 44}}. This seems to have led to a new Question: Why is it correct on iPhone but not on iPad?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template