Custom UIPickerView with Custom Background color

2019-01-22 15:31发布

I have created a sample picker-view which will display some details. Setting the Background color using:

_pickerView.backgroundColor = [UIColor redColor]; 

doesn't seems to be working . But I wish I could create a picker view like the below image

enter image description here

What I was trying to do is , the entire PickerView background color should not be white, it should fill with the UIColor which I am giving. Is that possible?? How it can be done ??

6条回答
迷人小祖宗
2楼-- · 2019-01-22 15:32

The API does not provide a way to change the style and color of selection dialer. You can change only inner view of the picker how u want to design. As the UIPickerView don't have the UI_APPEARANCE_SELECTOR.

Check out below link will help you out. Custom iOS UIDatepicker using UIAppearance

查看更多
可以哭但决不认输i
3楼-- · 2019-01-22 15:38

You can change selected color by putting tableview cell in it.

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;

if( cell == nil ) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    [cell setBackgroundColor:[UIColor redColor]];

    [cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
    cell.textLabel.text = [self.userNameArray objectAtIndex:row];
    cell.userInteractionEnabled = NO;
}

return cell;
}
查看更多
神经病院院长
4楼-- · 2019-01-22 15:42

It is hard to customize a UIPickerView.

You might opt to use it in its default appearance or trying to simulate a UIPickerView using another UIKit component such as UITableView. There are some libs that are using UITableView to simulate a more flexible UIPickerView.

查看更多
狗以群分
5楼-- · 2019-01-22 15:44

UIPickerView has subviews. Assigning an image to a view and adding it to the subview at index 2, would change the background of pickerview.

I had had a same kinda problem :-) See my question here!! Customize UIPickerView's Skin with images

Or you can use AFPickerView

查看更多
Melony?
6楼-- · 2019-01-22 15:53

Addition to Nina's answer, below are some of the good custom Picker view controls which will be good to use in terms of performance and customizable.

  1. http://www.cocoacontrols.com/platforms/ios/controls/cppickerview
  2. http://www.cocoacontrols.com/platforms/ios/controls/afpickerview
  3. http://www.cocoacontrols.com/platforms/ios/controls/v8horizontalpickerview (Horizontal PickerView)
查看更多
趁早两清
7楼-- · 2019-01-22 15:57

I wrote a custom picker view that uses a UITableView as it's starting point. Feel free to customize it however you want.

https://github.com/derekju/DjuPickerView

查看更多
登录 后发表回答