简体   繁体   English

如何为UISplitViewController的纵向模式下显示的UIBarButtonItem提供自定义视图?

[英]How to give a custom view for the UIBarButtonItem that is shown on portrait mode for UISplitViewController?

When we go portrait mode while using a UISplitViewController, they will provide us by a barButtonItem. 当我们使用UISplitViewController进入纵向模式时,它们将通过barButtonItem提供给我们。 How can I use a customView for that barButtonItem? 如何为该barButtonItem使用customView?

I tried the following way. 我尝试了以下方法。 It works if I start with portrait orientation. 如果我从纵向开始,它会起作用。 But if I go landscape and when I come back it crashes. 但是,如果我去风景,当我回来时,它就会崩溃。

In viewDidLoad 在viewDidLoad中

UIImage *image = [UIImage imageNamed:@"home.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, 22.00, 22.00);    
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(showHomeDetails) forControlEvents:UIControlEventTouchDown];    
homeButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];

Then in willHideViewController 然后在willHideViewController中

barButtonItem = homeButton;

What shud I do? 我要做什么 OR is there an alternate solution? 还是有替代解决方案?

Rewrote it like this inside the willHideViewController and its done. 这样将其重写为willHideViewController并完成。

UIImage *image = [UIImage imageNamed:@"news.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height );    
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(showSideTable) forControlEvents:UIControlEventTouchUpInside];    
[barButtonItem setCustomView:button];

Main change is removed [button release]. 主要更改已删除[按钮释放]。

Assuming an image is the custom view you're looking for: 假设图像是您要查找的自定义视图:

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    [barButtonItem setImage:[UIImage imageNamed:@"awesome_menu_icon.png"]];
    [[self navigationItem] setLeftBarButtonItem:barButtonItem];
}

I believe it should be this: 我相信应该是这样的:

UIImage *image = [UIImage imageNamed:@"home.png"];
UIImageView *imageView = [UIImageView initWithImage:image];
homeButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
[homeButton addTarget:self action:@selector(showHomeDetails)];
[homeButton setBounds:CGRectMake(0, 0, 22.00, 22.00)];

Hope this solves your problem 希望这能解决您的问题
jrtc27 jrtc27

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UISplitViewController 纵向模式缺少 UIBarButtonItem - UISplitViewController portrait mode missing UIBarButtonItem UISplitViewController设置为纵向模式 - UISplitViewController set in portrait mode UISplitViewController 始终在 iPad 纵向模式 iOS 9 中显示主视图 - UISplitViewController always show master view in iPad portrait mode iOS 9 UISplitViewController在横向模式下显示为纵向 - UISplitViewController appears portrait in landscape mode UISplitViewController - 以纵向模式打开,masterViewController可见 - UISplitViewController - open in portrait mode with masterViewController visible 在纵向模式下按下新视图控制器后,iOS UISplitViewController的Popover控制器按钮消失 - iOS UISplitViewController's Popover controller button disappear after pushing new view controller in portrait mode UISplitViewController中的ActionSheet在纵向模式下的行为与横向模式不同 - ActionSheet within UISplitViewController acts different in Portrait Mode than Landscape Mode 以纵向模式拆分视图! - Split view in portrait mode! UISplitViewController中的肖像:如何以编程方式隐藏主弹出窗口? - UISplitViewController in portrait: how to hide master popover programmatically? 当我在UISplitViewController中将iPad转到纵向模式时,调整detailViewController的pagecontrolview的大小 - Resize the pagecontrolview of detailViewController when i turn iPad to Portrait mode in UISplitViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM