gpt4 book ai didi

iphone - 如何使用包含彩色图像的按钮自定义 UIToolbar?

转载 作者:可可西里 更新时间:2023-11-01 05:04:55 24 4
gpt4 key购买 nike

我有两个关于 UIToolbar 的问题:

1:我已经阅读了许多关于如何在 UIToolbar 中使用带有自定义图像(彩色)的按钮的 Stackoverflow 答案。我试图在 UIToolbar 顶部放置一个 View (hack)并将带有图像的按钮放在其中,但失败了。现在我被困住了。你怎么能做到这一点?

2:有没有办法让多个按钮同时处于“按下”状态?我要实现的功能是不同的按钮进行不同种类的排序。

最佳答案

好的,答案自己解决了......这里是:

Can I have a UIBarButtonItem with a colored image?

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;

//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];

//Calculate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;

//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;

//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

//Reposition and resize the receiver
[toolbar setFrame:rectArea];

//Create a button
UIImage *image = [UIImage imageNamed:@"colorImage.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(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

[toolbar setItems:[NSArray arrayWithObjects:barButtonItem,nil]];

//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
}

-(void)myAction{
NSLog(@"jippiii");
}

关于iphone - 如何使用包含彩色图像的按钮自定义 UIToolbar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4666726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com