简体   繁体   中英

How to increase size of UIView inside viewDidLoad method?

I am very new in ios application. I have 3 views on my main view. These views are representing a graph. anytime user opens main view (I mean each time viewDidLoad gets called) I need to measure some numbers and draw these graphs according to them. I only need to change the width of the graph! However I tried to manually add size to one of the graph but did not change at all! any suggestion will be appreciated.

As you can see I have added 100 here

self.timerGraph.frame.size.width+100


self.timerGraph.frame = CGRectMake(self.timerGraph.frame.origin.x,self.timerGraph.frame.origin.y,self.timerGraph.frame.size.width+100,self.timerGraph.frame.size.height);

But it is not affected!

Edit: I used this but Ben is printing but the width is not changing!!

- (void)viewWillAppear:(BOOL)try{

    NSLog(@"Ben");
    self.bestGraph.frame = CGRectMake(self.bestGraph.frame.origin.x,self.timerGraph.frame.origin.y,self.timerGraph.frame.size.width+130,self.timerGraph.frame.size.height);

}

Edit: I have deleted some implementation parts to make it shorter.

- (void)viewDidLoad
{


    NSNumber *min = [NSNumber numberWithInt:0];
    NSNumber *max = [NSNumber numberWithInt:0];
    NSNumber *avg = [NSNumber numberWithInt:0];
    NSNumber *total = [NSNumber numberWithInt:0];


    [super viewDidLoad];
    bool first = true;
    //Find the worst and best and average graph
    for(int i=0;i<tmp.numberOfTimes;i++)
    {
        //LTTime *myTime = [[LTTime alloc] init];
        LTTime *tmpTime = [tmp timeAtIndex:i];
        if(first)
        {
            first = false;
            min = tmpTime.time;
        }
        if(min>tmpTime.time)
        {
            min = tmpTime.time;
        }
        if(max<tmpTime.time)
        {
            max = tmpTime.time;
        }
        total = @( [tmpTime.time intValue] + [total intValue] );

    }
    avg = @( [total floatValue]/tmp.numberOfTimes);

    self.bestGraph.frame = CGRectMake(self.bestGraph.frame.origin.x,self.timerGraph.frame.origin.y,self.timerGraph.frame.size.width+130,self.timerGraph.frame.size.height);

    }

Definition of view:

@property (weak, nonatomic) IBOutlet UIView *bestGraph;

在此处输入图片说明

Try to put your code in

 - (void)viewWillLayoutSubviews

method.

I don't know whether it is true according to your project.

Try the same code in viewWillAppear instead of viewDidLoad .

viewDidLoad calls only once when the app runs for the first time.

Hope it will helpful for you. Thank you :)

The problem was ios autolayout was ticked so I was not able to add in that way. So I decided to create my own view and anytime it goes to ViewWillAppear delete everything from view and add them again (because the view was changing more often).

I think it is better to learn how to create GUI elements by hand instead of IButton.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM