相关文章推荐
失落的汽水  ·  Building a Real-Time ...·  1 月前    · 
干练的甜瓜  ·  谢茂强-南开大学软件学院官网·  4 月前    · 
阳刚的饭卡  ·  Vue:通过浏览器控制台查看全局data值_ ...·  1 年前    · 
安静的铅笔  ·  kaggle下载不了output的权值文件, ...·  1 年前    · 
玉树临风的鸡蛋  ·  初学者随记07:VUE提取对象键名_vue获 ...·  2 年前    · 
Code  ›  grid网格布局实现图表框架(vue)_vue grid
vue
https://blog.csdn.net/KK_vicent/article/details/131231030
安静的火柴
1 年前
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
提问

问 定制UIPickerView (背景和间距)

Stack Overflow用户
提问于 2011-04-21 13:49:19
EN

我想将UIPickerView中的白色背景更改为我自己的图像。

这个是可能的吗?

此外,我还设法使我的UIPickerView水平滚动而不是垂直滚动。现在,我想知道是否有任何方法来调整选择器视图的两行之间的间距?

我附上了一张图片来说明我的意思。

这是我的密码:

代码语言: javascript
运行
复制
- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    arrayDays = [[NSMutableArray alloc] init];
    [arrayDays addObject:@"ONSDAG"];
    [arrayDays addObject:@"TORSDAG"];
    [arrayDays addObject:@"FREDAG"];
    [arrayDays addObject:@"LØRDAG"];
    arrayDates = [[NSMutableArray alloc] init];
    [arrayDates addObject:@"29. JUNI"];
    [arrayDates addObject:@"30. JUNI"];
    [arrayDates addObject:@"1. JULI"];
    [arrayDates addObject:@"2. JULI"];
    pickerViewDay = [[UIPickerView alloc] initWithFrame:CGRectZero];
    [pickerViewDay setDelegate:self];
    [pickerViewDay setShowsSelectionIndicator:NO];
    CGAffineTransform rotate = CGAffineTransformMakeRotation(-M_PI/2);
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
    [pickerViewDay setTransform:rotate];
    [pickerViewDay setCenter:CGPointMake(self.view.frame.size.width/2, (pickerViewDay.frame.size.height/2)-3)];
    [self.view addSubview:pickerViewDay];
    // Adding selection indicator to pickerview
    UIImage *selectorImage = [UIImage imageNamed:@"DayPickerView_SelectionIndicator.png"];
    UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
    [customSelector setFrame:CGRectMake(0, 0, 120, 74)];
    [customSelector setCenter:CGPointMake(self.view.frame.size.width/2, customSelector.frame.size.height/2)];
    [self.view addSubview:customSelector];
    [customSelector release];
    // Adding background to pickerview
    UIImage *backgroundImage = [UIImage imageNamed:@"DayPickerView_Background.png"];
    UIView *custombackground = [[UIImageView alloc] initWithImage:backgroundImage];
    [custombackground setFrame:CGRectMake(0, 0, 320, 74)];
    // [self.view addSubview:custombackground];
    [custombackground release];
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIView *viewRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
    CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
    // Date
    CGRect rectDate = CGRectMake(30, 0, 150, 80);
    UILabel *date = [[UILabel alloc]initWithFrame:rectDate];
    [date setTransform:rotate];
    [date setText:[arrayDates objectAtIndex:row]];
    [date setFont:[UIFont fontWithName:@"Arial-BoldMT" size:37.0]];
    [date setShadowColor:[UIColor whiteColor]];
    [date setShadowOffset:CGSizeMake(0, -1)];
    [date setTextAlignment:UITextAlignmentCenter];
    [date setBackgroundColor:[UIColor clearColor]];
    [date setClipsToBounds:YES];
    [viewRow addSubview:date];
    // Day
    CGRect rectDay = CGRectMake(-30, 0, 150, 80);
    UILabel *day = [[UILabel alloc]initWithFrame:rectDay];
    [day setTransform:rotate];
    [day setText:[arrayDays objectAtIndex:row]];
    [day setFont:[UIFont fontWithName:@"Arial-BoldMT" size:21.0]];
    [day setTextColor:[UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1]];
    [day setTextAlignment:UITextAlignmentCenter];
    [day setBackgroundColor:[UIColor clearColor]];
    [day setClipsToBounds:YES];
    [viewRow addSubview:day];
    return viewRow;
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [arrayDays objectAtIndex:row];
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayDays count];
}

​

​

编辑1

对于RickiG (背景):

​

​

编辑2

对于RickiG:

​

​

4 13.5K 0 票数 7
EN
iphone
sdk
background
uipickerview

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-04-21 14:59:38

嗨,没有直接的方式来改变背景。您可以做的是让您在viewForRow特性中返回的视图具有它自己的背景(如果需要的话,随后在每一侧添加阴影)。您也可以去寻找viewWithTag:但这绝不是一个好主意,因为这可能会在未来的iOS版本中发生变化。

您实现viewForRow和TitleForRow有什么特殊的原因吗?我通常只是在这个委托方法中填充viewForRow的标签。

viewForRow能够重用选择器中的视图,就像UITableView一样,您应该测试"reusingView:(UIView *)视图“是否为零,如果没有,则无需再次绘制所有内容。只是填充标签。

我通常从不自定义选择器,如果我需要一些不是完全自定义的东西--我对UITableView进行了子类化,那么它就会更加灵活,并且可以做更多的选择器。

对于可以使用行的“高度”的间距,选择器将在viewForRow中对返回的视图进行居中,然后确保:

代码语言: javascript
运行
复制
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

返回大于视图的值。

(持有og lykke;)

票数 6
EN

Stack Overflow用户

发布于 2012-03-15 16:48:26

我刚刚想出了如何在选择器视图中应用背景颜色或图像。希望能帮上忙。

只需在要选择器视图的文件中定义一个类别,如下所示:

代码语言: javascript
运行
复制
@implementation UIPickerView(Extension)
-(void)drawRect:(CGRect)rect
    NSArray* subviews = [self subviews];
    for(UIView* view in subviews)
        if([view isKindOfClass:[UITableView class]])
            view.backgroundColor = appColor;
 
推荐文章
失落的汽水  ·  Building a Real-Time Chat Micro-Frontend with Vue 3, Vite, and Module Federation
1 月前
干练的甜瓜  ·  谢茂强-南开大学软件学院官网
4 月前
阳刚的饭卡  ·  Vue:通过浏览器控制台查看全局data值_vue控制台如何查看数据-CSDN博客
1 年前
安静的铅笔  ·  kaggle下载不了output的权值文件,点击download没反应,如何解决? - 知乎
1 年前
玉树临风的鸡蛋  ·  初学者随记07:VUE提取对象键名_vue获取对象的键名_ChenMoJie_的博客-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号