UIBarButtonItem: How can I find its frame?

前端 未结 16 898
忘掉有多难
忘掉有多难 2020-12-12 23:17

I have a button in a toolbar. How can I grab its frame? Do UIBarButtonItems not have a frame property?

16条回答
  •  暖寄归人
    2020-12-12 23:56

    You can roughly calculate it by using properties like layoutMargins and frame on the navigationBar, combined with icon size guides from Human Interface Guidelines and take into count the current device orientation:

    - (CGRect)rightBarButtonFrame {
        CGFloat imageWidth = 28.0;
        CGFloat imageHeight = UIDevice.currentDevice.orientation == UIDeviceOrientationLandscapeLeft || UIDevice.currentDevice.orientation == UIDeviceOrientationLandscapeRight ? 18.0 : 28.0;
        UIEdgeInsets navigationBarLayoutMargins = self.navigationController.navigationBar.layoutMargins;
        CGRect navigationBarFrame = self.navigationController.navigationBar.frame;
        return CGRectMake(navigationBarFrame.size.width-(navigationBarLayoutMargins.right + imageWidth), navigationBarFrame.origin.y + navigationBarLayoutMargins.top, imageWidth, imageHeight);
    }
    

提交回复
热议问题