Setting the accessibilityFrame parameter of the element whose parent view will be moved

I have a UITableView with a custom header (i.e. I create a UIView myself). I need to configure the accessibilityFrame interface of one of the view subzones, but I cannot figure out how to set the frame coordinates correctly - they should be relative to the window, but I'm not sure how to do this.

My code looks like

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section { CGRect bounds = CGRectMake(0, 0, [tableView frame].size.width, 48); UIView *header = [[UIView alloc] initWithFrame:bounds]; UILabel *labelOne = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, bounds.size.width - 80, 18)]; UILabel *labelTwo = [[UILabel alloc] initWithFrame: CGRectMake(0, 20, bounds.size.width - 80, 18)]; CGRect frameOne = [labelOne frame]; CGRect frameTwo = [labelTwo frame]; [labelTwo setIsAccessibilityElement:NO]; [labelOne setAccessibilityFrame:CGRectUnion(frameOne, frameTwo)]; // ... return header; } 

I have two UILabels that I want to merge into one for VoiceOver purposes. I do this by ignoring the second label and expanding the frame of the first label to cover the area of ​​the second label. (The second label is immediately lower than the first.) The problem is getting frames. If I use the code as shown above, the accessibility frame is the correct size, but it is positioned as if the UITableViews header was in the upper left corner of the screen. I tried changing the code to say

 CGRect frameOne = [header convertRect:[labelOne frame] toView:nil]; CGRect frameTwo = [header convertRect:[labelTwo frame] toView:nil]; 

but the same thing happened. Should this last piece of code convert UILabels frames to relative window coordinates?

I thought that maybe the problem is that when the UIView is created, it does not know where it will be positioned on the screen (and as part of the UITableView it can scroll everywhere). Do I need to implement accessibilityFrame as a message that checks the position of UIViews every time it is called?

+4
source share
2 answers

There's a helper function here to help you do just that: UIAccessibilityConvertFrameToScreenCoordinates . This function takes a CGRect and converts it from the view coordinate system to the screen coordinates.

+1
source

I do not think this is the time when UIView is being created, since I believe that window should not be nil by the time tableView:viewForHeaderInSection: called. I think the problem is with the receiver of the convertRect:toView: message. Instead of passing this message to header , you should pass it [self view] .

You are moving from the receiver coordinate system to another view, in this case nil or UIWindow in your application. When header receives this message, you switch from the coordinate system header to the coordinate system window , but header itself is a subzone [self view] . Instead, you want to ask [self view] to do the conversion, which should take into account any UINavigationBar , etc.

 CGRect frameOne = [[self view] convertRect:[labelOne frame] toView:nil]; CGRect frameTwo = [[self view] convertRect:[labelTwo frame] toView:nil]; 
0
source

Source: https://habr.com/ru/post/1402009/


All Articles