在所有屏幕中用背景颜色替换背景图像

时间:2014-08-08 05:50:50

标签: background-color uicolor

我在开发我的应用程序时遇到了一个问题。在我的应用程序中,有超过10个屏幕并完成了设计。但现在我想改变屏幕的整个颜色,同时用户可以打开颜色选择器和选择的颜色将适用于所有的screes。

任何人都可以建议我怎样才能做到这一点。因为我也使用过很多的imageview和图像。

下面是我在uitableview单元格中使用图像的示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CategoriesListCell";

    CategoriesListCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {

        cell = [[CategoriesListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        [cell.contentView setBackgroundColor:[UIColor clearColor]];
        [cell setBackgroundColor:[UIColor clearColor]];

        int row = indexPath.row * 2;

        NSLog(@"row:::%d",row);


        UIButton *btnCategoriesLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, btnWidth, rowHeight)];
        UIImage *imgButton = [UIImage imageNamed:@"bgbutton"];
        [btnCategoriesLeft setImage:imgButton forState:UIControlStateNormal];
        [btnCategoriesLeft addTarget:self action:@selector(btnCatTapped:) forControlEvents:UIControlEventTouchUpInside];
        [btnCategoriesLeft setTag:row];

        //Set Transpernt images
        UIImageView *imgViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 88, 53)];
        [imgViewLeft setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",row]]];
        [imgViewLeft setBackgroundColor:[UIColor clearColor]];
        [btnCategoriesLeft addSubview:imgViewLeft];


        //Set Title of label
        UILabel *lblCategoriesTitleLeft = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 140, 20)];
        lblCategoriesTitleLeft.backgroundColor = [UIColor clearColor];
        lblCategoriesTitleLeft.textColor = [UIColor whiteColor];
        [lblCategoriesTitleLeft setFont:FONT_WITH_SIZE_REGULAR(15.0)];
        [lblCategoriesTitleLeft setText:[arrListOfCategories objectAtIndex:row]];
        [lblCategoriesTitleLeft setTextAlignment:NSTextAlignmentCenter];
        [btnCategoriesLeft addSubview:lblCategoriesTitleLeft];
        [[cell contentView] addSubview:btnCategoriesLeft];


        UIButton *btnCategoriesRight = [[UIButton alloc] initWithFrame:CGRectMake(btnWidth, 0, btnWidth, rowHeight)];
        imgButton = [imgButton stretchableImageWithLeftCapWidth:imgButton.size.width/2 topCapHeight:imgButton.size.height/2];
        [btnCategoriesRight setImage:imgButton forState:UIControlStateNormal];
        [btnCategoriesRight setTag:row +1];
        [btnCategoriesRight addTarget:self action:@selector(btnCatTapped:) forControlEvents:UIControlEventTouchUpInside];


        //Set Transpernt images
        UIImageView *imgViewRight = [[UIImageView alloc] initWithFrame:CGRectMake(40, 30, 88, 53)];
        [imgViewRight setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",row+1]]];
        [imgViewRight setBackgroundColor:[UIColor clearColor]];
        [btnCategoriesRight addSubview:imgViewRight];


        //Set Title of label
        UILabel *lblCategoriesTitleRight = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 140, 20)];
        lblCategoriesTitleRight.backgroundColor = [UIColor clearColor];
        lblCategoriesTitleRight.textColor = [UIColor whiteColor];
        [lblCategoriesTitleRight setFont:FONT_WITH_SIZE_REGULAR(15.0)];
        [lblCategoriesTitleRight setText:[arrListOfCategories objectAtIndex:row+1]];
        [lblCategoriesTitleRight setTextAlignment:NSTextAlignmentCenter];
        [btnCategoriesRight addSubview:lblCategoriesTitleRight];

        [[cell contentView] addSubview:btnCategoriesRight];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;

}

感谢。

1 个答案:

答案 0 :(得分:0)

我对您的示例代码如何与您提出的问题相关联感到困惑。如果我理解正确,你想用滑块改变所有10个或更多屏幕的背景颜色。尝试设置红色,蓝色和绿色滑块,并通过app delegate分配全局变量。

将三个滑块添加到界面构建器。 然后设置三个UISlider插座。

<强> YourSliderViewController.h

@interface YourViewController ()
@property (nonatomic, weak) IBOutlet UISlider *redSlider;
@property (nonatomic, weak) IBOutlet UISlider *blueSlider;
@property (nonatomic, weak) IBOutlet UISlider *greenSlider;
@end

<强> YourSliderViewController.m

- (IBAction)changeColor:(id)sender{
float red = self.redSlider.value;
float green = self.greenSlider.value;
float blue = self.blueSlider.value;
UIColor *newColor = [UIColor colorWithRed:red
                                    green:green
                                     blue:blue
                                    alpha:1.0];
self.view.backgroundColor = newColor;
}

以下是使您的RGB滑块可供所有类访问。这样你就可以在-viewDidLoad中的任何其他ViewController中设置self.view.background = newColor。 创建一个指向AppDelegate的指针。我打电话给我的appDel。

<强> MyXcodeProject-Prefix.pch

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#import "AppDelegate.h"
AppDelegate *appDel;
#endif

<强> AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIColor *newColor;

@end

<强> AppDelegate.m

appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];