Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:37:40+00:00 2026-05-27T11:37:40+00:00

I know there have been plenty of variations on this question before, but none

  • 0

I know there have been plenty of variations on this question before, but none seem to capture the particular issue I’m having. I’m new to iOS development and Objective-C, so please bear with me if this is a particularly basic oversight on my part.

I’m trying to dynamically change the background color of a view (it’s a single view application.) I can easily change the background color through statements like:

[self.view setBackgroundColor:[UIColor blueColor]];

or

UIColor *newColor = [[UIColor alloc] initWithRed:.777777 green:.777777 blue:.777777 alpha:.777777];
[self.view setBackgroundColor:newColor];

I run into trouble when I try to change one of those RGBA values dynamically (using sliders, in this case.) I have four sliders, with tags 0-3, hooked up via this method:

- (IBAction)sendColor:(id)sender {
    UISlider *slider = (UISlider *)sender;
    CGFloat newValue = (CGFloat)(slider.value);
    int senderTag = [sender tag];
    CGFloat *components = CGColorGetComponents(self.view.backgroundColor.CGColor);
    components[senderTag] = newValue;
    UIColor *newColor = [[UIColor alloc] initWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
    [self.view setBackgroundColor:newColor];
    NSString *colorAsString = [NSString stringWithFormat:@"%f, %f, %f, %f", components[0], components[1], components[2], components[3]];
    NSLog(@"%@", colorAsString);
}

When I move a slider, I can see the values updating properly via NSLog:

2011-12-13 20:54:26.468 HelloWorld1[1283:707] 0.739568, 0.865854, 1.000000, 1.000000

…but the background color never changes. I feel like I’m missing something rudimentary here, do you have any suggestions? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:37:41+00:00Added an answer on May 27, 2026 at 11:37 am

    Don’t call CGColorGetComponents. Rather, create a CGFloat components[4] ivar in your class and set this directly in the init.

    Example…

    @interface MyView : UIView
    {
        CGFloat components[4];
    }
    @end
    

    …

    -(id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self != nil)
        { 
            components[0] = 1;
            components[1] = 1;
            components[2] = 1;
            components[3] = 1;
    
            self.view.backgrounColor = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
        }
        return self;
    }
    

    Then change only the component that you need to.

    - (IBAction)sendColor:(id)sender {
        UISlider *slider = (UISlider *)sender;
        CGFloat newValue = (CGFloat)(slider.value);
        int senderTag = [sender tag];
        components[senderTag] = newValue;
        UIColor *newColor = [[UIColor alloc] initWithRed:components[0] green:components[1] blue:components[2] alpha:components[3]];
        [self.view setBackgroundColor:newColor];
        NSString *colorAsString = [NSString stringWithFormat:@"%f, %f, %f, %f", components[0], components[1], components[2], components[3]];
        NSLog(@"%@", colorAsString);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there have been quite a few posts on this but none seem
I know there have been some questions about this, but I can't seem to
I know there have been plenty of questions about this, but I've been trying
I know that there have been plenty of topics describing this topic but I
Okay, i know there have been some questions about this before, but I just
I know there have been a few other people asking this question, but my
I know there have been a few threads on this before, but I have
I know there have been some similar questions to this, but they haven't helped
I know there have been a million questions asking this, but mine is different.
I know there are tons of threads regarding this issue but I have not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.