Rotate UIBarButtonItem

我是研究僧i 提交于 2019-12-30 10:53:09

问题


I want to rotate a UIBarButtonItem.

I have been able to do this with UIButtons using setTransform:CGAffineTransformMakeRotation(…), but UIBarButtonItem does not have this property.

I know the Camera-app does this, so it should be achieveable.
How can I achieve this?

Thanks.


回答1:


Have you tried using a custom view inside the UIBarButtonItem that you then transform any way you want?




回答2:


UIBarButtonItem does not extend UIView, so it cannot be transformed directly. You can add the UIBarButtonItem you wish to transform to a UIToolbar, transform the UIToolbar and then add the toolbar as a custom view to another UIBarButtonItem. This item can then be set as a navigation item or added to another UIToolbar. However, if you are using a custom view or image then Emil's approach in the comment above is best.

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(handleForwardItemTouch:)];

UIToolbar *backToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
[backToolbar setTransform:CGAffineTransformMakeScale(-1, 1)];

UIBarButtonItem *backToolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:backToolbar] autorelease];
self.navigationItem.rightBarButtonItem = backToolbarItem;



回答3:


You could put a UIButton inside a Bar Button Item, than rotate the UIButton.




回答4:


I extended UIToolBar, giving access to its subviews, and in it have a function rotate that rotates the buttons in the opposite direction of the bar:

- (void)rotate: (int)degrees{
    //for the bar
    self.transform=CGAffineTransformMakeRotation(DegreesToRadian(degrees));
    //for the subviews (UIBarButtonItems)
    for (UIView * subView in self.subviews)
    {
        if(!CGRectEqualToRect(subView.bounds, self.bounds))
            subView.transform =   CGAffineTransformMakeRotation(DegreesToRadian(-degrees));
    }

} 


来源:https://stackoverflow.com/questions/4115417/rotate-uibarbuttonitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!