2
votes

I am creating UIButton dynamically in the FOR loop as follows :

     CGRect workingFrame = imgscrollView.frame;
      workingFrame.origin.x = 0;
      workingFrame.origin.y = 0;

   for (int i=0 ; i < self.currentDetails.arrayOfImages.count ; i++)
   {
    UIButton *imageBtn = [[UIButton alloc] init];
    [imageBtn setImage:image forState:UIControlStateNormal];
    [imageBtn setUserInteractionEnabled:TRUE];
    imageBtn.layer.cornerRadius = 8;
    imageBtn.layer.borderWidth = 1;
    imageBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    imageBtn.layer.masksToBounds = YES;
    imageBtn.clipsToBounds = YES;
    [imageBtn setContentMode:UIViewContentModeScaleAspectFill];
    [imageBtn addTarget:self action:@selector(changeButtonImage:) forControlEvents:UIControlEventTouchUpInside];
    [imageBtn setTag:i];
    [imgscrollView addSubview:imageBtn];

     imageBtn.frame = CGRectMake(workingFrame.origin.x+20, workingFrame.origin.y, 145, 140);
   }

But at the time of setting its frame

imageBtn.frame = CGRectMake(workingFrame.origin.x+20, workingFrame.origin.y, 145, 140);

i am getting following error and it crashes :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized selector sent to instance

I have searched for this but could not get the solution.

Please help me.

Thanks...

1
Valid stack trace please?CodaFi
Which exact line causes the crash?rmaddy
as an aside, if you are getting your scroll view's frame then setting the origin to (0,0) perhaps you should have just retrieved its bounds in the first place. Difference between frame and bounds seems oft misunderstood.RobP
The problem may be caused by the image you set on the button. Verify that image is REALLY a UIImage and not actually an NSString.rmaddy
@Rohan the bug is somewhere else in your code. Where are you creating image for example?Abhi Beckert

1 Answers

1
votes

Size method is there for the classes that are in the image attached

enter image description here

Since you are using image here check whether you have a proper image instance.