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 7751415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:32:31+00:00 2026-06-01T11:32:31+00:00

I’m trying to create a CGRect inside of a custom view (rectView) that will

  • 0

I’m trying to create a CGRect inside of a custom view (rectView) that will move up and down as you move a slider.

My slider’s IBAction calls the following method: (which get’s called fine)

- (void)moveRectUpOrDown:(int)y
{
    self.verticalPositionOfRect += y;
    [self setNeedsDisplay];
}

My drawRect method:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat size = 100;
    self.rect = CGRectMake((self.bounds.size.width / 2) - (size / 2), 
                                 self.verticalPositionOfRect - (size / 2), 
                                 size, 
                                 size);
    CGContextAddRect(context, self.rect);
    CGContextFillPath(context);
}

My custom view’s initWithFrame calls the drawRect method using setNeedsDisplay, but for some reason the moveRectUpOrDown won’t call drawRect.

Any ideas what I’m doing wrong?

For clarity the entire implementation is below:

//ViewController.h
#import <UIKit/UIKit.h>
#import "rectView.h"

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet rectView *rectView;
- (IBAction)sliderChanged:(id)sender;
@end

//ViewController.m
#import "ViewController.h"

@implementation ViewController
@synthesize rectView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.rectView = [[rectView alloc] initWithFrame:self.rectView.frame];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)sliderChanged:(id)sender 
{
    UISlider *slider = sender;
    CGFloat sliderValue = slider.value;
    [self.rectView moveRectUpOrDown:sliderValue];
}
@end

//rectView.h
#import <UIKit/UIKit.h>

@interface rectView : UIView
- (void)moveRectUpOrDown:(int)y;
@end

//rectView.m
#import "rectView.h"

@interface rectView ()
@property CGRect rect;
@property int verticalPositionOfRect;
@end

@implementation rectView
@synthesize rect, verticalPositionOfRect;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.verticalPositionOfRect = (self.bounds.size.height / 2);
        [self setNeedsDisplay];
    }
    return self;
}

- (void)moveRectUpOrDown:(int)y
{
    self.verticalPositionOfRect += y;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat size = 100.0;
    self.rect = CGRectMake((self.bounds.size.width / 2) - (size / 2), 
                                 self.verticalPositionOfRect - (size / 2), 
                                 size, 
                                 size);
    CGContextAddRect(context, self.rect);
    CGContextFillPath(context);
}

@end

Thanks for the help 🙂

  • 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-06-01T11:32:32+00:00Added an answer on June 1, 2026 at 11:32 am

    OK so the answer is the view is never actually added to the viewController‘s view..

    You have an IBOutlet to your rectView – so I assume you dragged a UIView component onto the view in Interface Builder and then changed it’s class to rectView, which is all fine BUT in viewDidLoad you wipe over this object with a new one

    self.rectView = [[rectView alloc] initWithFrame:self.rectView.frame];
    

    So this is now no longer the object that was loaded from the xib.

    Subsequently this view is never actually added to the view hierarchy so it’s never going to draw itself as it does not make sense.

    So the solution is to remove the line I highlighted above.


    Notes

    I would probably still go with my other answer for such an implementation but it’s helpful to get tripped up and learn new things.

    rectView does not follow naming conventions. Ideally you should start your class name with a 2-3 letter prefix (possibly your initial’s or company name) followed by a camel cased named.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.