Increase UIView size not working - ios

I have a NSArray of UIViews, and i am trying to increase the size of all of them, i am using the below but it is not working?
for (UIView *view in self.viewArray) {
int rndValue = 1 + arc4random() % (360 - 1);
view.layer.affineTransform = CGAffineTransformMakeRotation(rndValue);
CGRect newFrame = view.frame;
newFrame.size.width = 600;
newFrame.size.height = 600;
[view setFrame:newFrame];
}
Any ideas? the rotate works but not increase. Might the constraints in storyboard be stopping it from working?

You shouldn't mix Auto Layout with setting frame sizes manually.
In your Storyboard map the constraints you want to change to IBOutlets and then change those programmatically instead.

Related

Autolayout with dynamic height

I am trying to create a subclass of UIView that will show a list of fixed sized UIImages similar to how a UILabel displays letters.If all the images won't fit on one line, the images are arranged on multiple lines.
How can I achieve this using autolayouts so that if I put this view in a UIStackView the images will be listed correctly?
Here is a sample if I did it using fixed position :
- (void) layoutSubviews{
[super layoutSubviews];
CGRect bounds = self.bounds;
CGRect imageRect = CGRectMake(0, 0, 30, 30);
for (UIImageView* imageView in self.imageViews){
imageView.frame = imageRect;
imageRect.origin.x = CGRectGetMaxX(imageRect);
if (CGRectGetMaxX(imageRect) > CGRectGetMaxX(bounds)){
imageRect.origin.x = 0.0;
imageRect.origin.y = CGRectGetMaxY(imageRect);
}
}
}
Update:
Here is a sample project to show the issue.
https://github.com/datinc/DATDemoImageListView
Here the link for ImageListView
https://github.com/datinc/DATDemoImageListView/blob/master/DATDemoImageListView/DATDemoImageListView.m
You should overwrite intrinsicContentSize returning the preferred content size of your view.
The problem is that in intrinsicContentSize the view has not it's final bounds. You can use an internal height constraint, and overwrite updateConstraints:
- (void)updateConstraints {
CGFloat theWidth = CGRectGetWidth(self.bounds);
NSUInteger theCount = [self.subviews count];
CGFloat theRows = ceilf(theCount / floorf(theWidth / 30.0));
self.heightConstraint.constant = 30.0 * theRows;
[super updateConstraints];
}
In viewDidAppear: and on layout changes (e. g. rotation) you have to call setNeedsUpdateConstraints to get a proper initial layout of the image views.

Implementing iPad General Setting Page

So far I have customized the tableview and implemented the iPad General Setting Page. Below is the code for tableview which will change frame accordingly. But the issue is when I insert/delete rows or section in the tableview. My tableviewcell backgroundview (not cell) width get shrinks. Any idea what wrong am I doing here?
- (void)setFrame:(CGRect)frame
{
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
CGFloat inset =10;
frame.origin.x += inset;
frame.size.width -= 2 * inset;//The issue is in this line
}
[super setFrame:frame];
}
I found the simple solution to achieve this. No need of customizing UITableView subclass. Just take the outlet of tableview and set the frame and change the color of backgroundview. like that below:-
CGFloat tableBorderLeft = 10;
CGFloat tableBorderRight = 10;
CGRect tableRect = self.view.frame;
tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
yourTableView.frame = tableRect;
self.view.backgroundColor=[UIColor colorWithRed:(239/255.0f) green:(239/255.0f) blue:(244/255.0f) alpha:1.0];

Autolayout UITextView inside UIScrollView size not correct

So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.

Unwanted bounce of subviews on scrollview

I have a scrollview that acts as a banner with 15 image views as subviews (scrolled horizontally). I add the subviews this way:
for (int i = 0; i < featuredImages.count; i++) {
CGRect frame;
frame.origin.x = self.scrollViewFeaturedImages.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollViewFeaturedImages.frame.size;
UIImageView *subview = [[UIImageView alloc] init];
subview.frame = frame;
[self.scrollViewFeaturedImages addSubview:subview];
}
And set the contentSize accordingly:
self.scrollViewFeaturedImages.contentSize = CGSizeMake(self.scrollViewFeaturedImages.frame.size.width * featuredImages.count, self.scrollViewFeaturedImages.frame.size.height);
self.scrollViewFeaturedImages.contentInset = UIEdgeInsetsZero;
However, when the view appears, the first image seems to be a little off.
When I scroll (horizontally) to the next image, the gap disappears, and when I scroll back to the first image, the frame is corrected already.
I've also disabled BOUNCE but I can drag the image vertically.
I've also tried this:
self.automaticallyAdjustsScrollViewInsets = NO;
with no success.
What's going on?
After being puzzled for 2 weeks I finally got this fixed by adding these lines in viewDidAppear.
self.scrollViewFeaturedImages.contentInset = UIEdgeInsetsZero;
self.scrollViewFeaturedImages.scrollIndicatorInsets = UIEdgeInsetsZero;
Did you try to change the UIImageView's property contentMode to UIViewContentModeScaleToFill?

UITextView contentSize automatically decreases

I have a UITextView whose height I am trying to resize as the number of lines increases. Apart form this, I need to resize the UIView in which the UITextView is contained. I am using the following code for this:
-(void) textViewDidChange:(UITextView *)textView{
CGFloat before = textView.frame.size.height;
CGRect frame = textView.frame;
frame.size = textView.contentSize;
CGFloat after = frame.size.height, diff = after - before, final = 0;
if (diff>16) {
final = 8;
}else if(diff<-1){
final = -16;
}else{
final = 0;
}
[self.containerView setFrame: CGRectMake(self.containerView.frame.origin.x,
self.containerView.frame.origin.y-final, self.containerView.frame.size.width,
frame.size.height+13)];
[textView setFrame: frame];
}
I am also trying to change the Y-position of the container as the text is changed. I am using static number values according to font size to avoid complications.
But the problem is that whenever the UITextView enters a new line, it resizes its contentSize to the original size and so the frame of the UITextView is also reduced for a moment, which looks really ugly. Also, the Y-position depends on this contentSize change and it gets all messed up when this automatic resize happens.
I am looking for the same behaviour which apps like whatsapp or viber have. Any idea how I can achieve this?
If I understand correctly, the containerView includes a textfield and must be drawn around it, regardless of the size of the textfield? If that is, try something like this
-(void) textViewDidChange:(UITextView *)textView
{
CGRect frame = textView.frame;
frame.size = textView.contentSize;
//do your magic calculations
[textView setFrame: frame];
[self.containerView setFrame: CGRectMake(self.containerView.frame.origin.x,
self.containerView.frame.origin.y-final, self.containerView.frame.size.width, frame.size.origin.y +frame.size.height+13)];
}

Resources