2012-01-05 38 views
10

Tôi muốn tùy chỉnh tiêu đề phần TableView và để lại màu nền mặc định. Tôi sử dụng - phần (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger). Tôi cần phải thay đổi kích thước phông chữ tùy thuộc vào thiết bị (iPad hoặc iPhone). Đối với mục đích này, hãy gọi hàm tiếp theo:Màu nền tiêu đề của phần TableView mặc định trên iPhone là gì?

[Màu UICWithHue: 0.6 độ bão hòa: 0,33 độ sáng: 0,69 alpha: 0,6].

Nhưng tôi đã tìm thấy các giá trị này theo cách thủ công.

Trả lời

23

Màu nền tiêu đề phần xem trước mặc định cho ios7 là

Objective-C 
[UIColor colorWithRed:247/255.0f green:247/255.0f blue:247/255.0f alpha:1.0f] 

Swift 
UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1) 
+0

Như tất cả các thành phần màu sắc giống hệt nhau (247/255), một sự thay đổi đơn giản hơn người bản xứ là: 'UIColor (trắng: 0,97, alpha: 1)' nơi 0,97 ~ 247/255 –

0

Những gì tôi đã làm trong một trong những ứng dụng của tôi là tạo ra các văn bản như vậy:

NSString *sectionTitle = @"YOUR TITLE HERE"; 
CGSize sz = [sectionTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0f] 
        constrainedToSize:CGSizeMake(290.0f, 20000.0f) 
         lineBreakMode:UILineBreakModeWordWrap]; 

CGFloat height = MAX(sz.height, 20.0f); 

CGRect sectionFrame = CGRectMake(0.0, 0.0, 320.0, height); 

tôi đã sử dụng 290 cho chiều rộng hạn chế để cung cấp cho học sinh nội trú nhãn ở hai bên. sau đó tôi đã sử dụng một hình ảnh căng ra như thế này:

UITableViewHeaderImage

Và quy mô nó để phù hợp với văn bản trong tiêu đề:

UIImage *headerImage = [UIImage imageNamed:@"sectionheaderbackground.png"]; 
    UIImage *stretchableImage = [headerImage stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:sectionFrame]; 
    backgroundImageView.image = stretchableImage; 

    // Add it to the view for the header 
    UIView *sectionView = [[UIView alloc] initWithFrame:sectionFrame]; 
    sectionView.alpha = 0.9; 
    sectionView.backgroundColor = [UIColor clearColor]; 
    [sectionView addSubview:backgroundImageView]; 

Cuối cùng, tôi đã tạo ra một nhãn và thêm nó như là một subview:

CGRect labelFrame = CGRectMake(10.0, 0.0, 290.0, height); 
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:labelFrame]; 
    sectionLabel.text = sectionTitle; 
    sectionLabel.numberOfLines = 0; 
    sectionLabel.font = [UIFont boldSystemFontOfSize:18.0]; 
    sectionLabel.textColor = [UIColor whiteColor]; 
    sectionLabel.shadowColor = [UIColor grayColor]; 
    sectionLabel.shadowOffset = CGSizeMake(0, 1); 
    sectionLabel.backgroundColor = [UIColor clearColor]; 
    [sectionView addSubview:sectionLabel]; 

    return sectionView; 
3

Đối Sự thay đổi màu nền của TableView Tiêu đề Mục Just One Dòng thứ thêm TableView đại biểu willDisplayHeaderView:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { 

    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
    **header.tintColor = [UIColor whiteColor];** 

} 
1

Objective-C:

[UIColor colorWithRed:232/255.0f green:233/255.0f blue:237/255.0f alpha:1.0f] 

Swift:

UIColor(red: 232/255, green: 233/255, blue: 237/255, alpha: 1) 
+0

Đây là dành cho iOS 9 trở lên –

Các vấn đề liên quan