0

I've drawn a UIView (scrollInner) in Interface Builder and I now want to increase its height programmatically and also change its background colour.

The UIView is inside a UIScrollView, and the scrolling all works fine. Basically, what I'm doing is trying to increase the inner UIView so it matches the contentSize of the UIScrollView.

Here's what I've got so far in my viewDidAppear method:

CGRect scrollInnerRect = self.scrollInner.frame;
scrollInnerRect.size.height = 1000;
self.scrollInner.frame = scrollInnerRect;
[self.scrollInner setBackgroundColor:[UIColor redColor]];

The background colour of the UIView changes to red as expected, but the height of the 'UIView' remains the same size as it was set in Interface Builder.

However, if I do this:

NSLog(@"My uiview's frame is: %@", NSStringFromCGRect(scrollInner.frame));

I get this in the log:

My uiview's frame is: {{0, 150}, {320, 1000}}

...which suggests that the UIView's height has been increased.

But that's not how it appears in the simulator. In other words, on screen the UIView is coloured red, but DOES NOT have the new height of 1000px - it's still way shorter.

Any ideas where I might be going wrong?

Thx.

Contention
  • 539
  • 5
  • 19

4 Answers4

0

check whether u have Autolayout enabled if so remove the spacing .and incerase the height of subview by view.frame = CGrectMake(x,y,yourwidth,yourincreasedheight)

naveen
  • 69
  • 7
  • Thanks - what do you mean by 'remove the spacing'? I've also tried increasing the height in the way you describe and I get the same result. – Contention Nov 29 '13 at 10:21
  • you have autolayout enabled ? – naveen Nov 29 '13 at 10:53
  • it seems your uiview is connected to other views using autolayout . check to which one it is and remove the clipping to that view.You have to remove the constraints try[yourview removeConstraints:yourview.constraints]; – naveen Nov 29 '13 at 10:59
  • thanks, it works as expected with autolayout off. But I need autolayout on to manage other things in the view. How do I check to see which other views the UIView is connected to? – Contention Nov 29 '13 at 11:07
0

You can add connection in interface builder from your UIView to your class extension to create properties. And after that you can make the changes in code like that.

self.myView.frame = self.scrollInner.contentSize.

And you can change your UIView colour like that:

[self.myView setBackgroundColor:[UIColor redColor]];
Greg
  • 25,317
  • 6
  • 53
  • 62
  • Thanks - I have no problem with changing the colour. My problem is that the height of the subview won't change! – Contention Nov 29 '13 at 10:24
  • Try replace this line self.scrollInner.frame = scrollInnerRect; with that line: self.scrollInner.contentSize = scrollInnerRect; – Greg Nov 29 '13 at 10:27
  • That won't work as UIView objects don't have a contentSize property. – Contention Nov 29 '13 at 10:34
  • Sorry I thought that scrollInner is in type of UIScrollView. can you make sure that your scrollView content size is set with height = 1000 as well. I believe your uiview has height = 1000 but your scrollView had content size less than that and you cannot see it. – Greg Nov 29 '13 at 10:38
  • Sorry, yes, scrollInner is a UIView. I have already set the UIScrollView's contentSize property to a height of 1000 which is equal to the desired height of the inner UIView, and I can definitely scroll to that distance - so that does work as expected. The problem is that scrollInner's height does not increase. – Contention Nov 29 '13 at 10:45
  • Some more info - the UIView has a height of around 400px in IB. I'm trying to change this to 1000px so it matches the UIScrollView's contentSize, and turn the background red. Once I scroll past the 400px position, the UIView ends and I see the superView's background through the transparent UIScrollView. Basically, I need the UIView to hide this! – Contention Nov 29 '13 at 10:48
  • Make sure your UIView in storyboard is set up right. Maybe There is some constraint which cause this issue. – Greg Nov 29 '13 at 11:44
0

Disable the 'use Autolayout' property in the identity inspector check your outlets properly in the xib file after that write this code :

CGRect scrollInnerRect = self.scrollInner.frame;

scrollInnerRect.size.height = CGrectMake(x,y,320,1000);
Gurpreet
  • 181
  • 6
0

You might have fixed this issue. This may help others.

We can create and hold the reference of NSLayoutConstraint for the view height.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint * scrollInnerHeightConstraint;

And we can use this height constraint to update the view height wherever we need

scrollInnerHeightConstraint.constant = 1000;//1000 is your height
scrollInnerHeightConstraint.priority = 1000;//1000 is High priority
ganka
  • 191
  • 1
  • 11
  • I have the same problem - the frame of the UIView is changed correctly, but the "expanded" part of the UIView is transparent. How does creating an NSLayoutConstraint property help? – thomers Jun 19 '18 at 10:08