Hi I tried to assign new frame size to the separator uiview, but it doesn't change, i dunno why, I tried to debug and the newFrame have a new value but after assign to separator, the separator seem like never receive the new value
UIView *separator = (UIView *)[cell viewWithTag: 10];
CGRect newFrame;
if(lblBuyPrice.frame.origin.x > lblBuyQuantity.frame.origin.x){
newFrame = CGRectMake( lblBuyQuantity.frame.origin.x, separatorBuy.frame.origin.y, lblBuyQuantity.frame.size.width, separatorBuy.frame.size.height);
}else{
newFrame = CGRectMake( lblBuyPrice.frame.origin.x, separatorBuy.frame.origin.y, lblBuyPrice.frame.size.width, separatorBuy.frame.size.height);
}
separatorBuy.frame = newFrame;
the uiview element is inside a cell
Related
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.
I'm trying to change the position of a UILabel contained inside a custom UITableViewCell, but it doesn't move. I have auto layout enabled.
- (void)updateLayout
{
NSArray* comp = [self.accessibilityHint componentsSeparatedByString:#"/"];
if (comp.count > 1)
{
CGRect rect = _imageViewT.frame;
rect.origin.x = 20 * comp.count;
rect.size.width = 44.0;
[_imageViewT setFrame:rect];
[_imageConstraint setConstant:_imageViewT.frame.origin.x-8];
rect = _lableT.frame;
rect.origin.x = _imageViewT.frame.size.width+_imageViewT.frame.origin.x+8;
rect.size.width = self.frame.size.width-rect.origin.x-40;
[_lableT setFrame:rect];
//[_imageConstraint setConstant:_imageViewT.frame.origin.x-8];
}
else
{
CGRect rect = _imageViewT.frame;
rect.origin.x = 10;
rect.size.width = 43.0;
[_imageViewT setFrame:rect];
[_imageConstraint setConstant:2];
rect = _lableT.frame;
rect.origin.x = 62;
rect.size.width = self.frame.size.width-rect.origin.x-40;
[_lableT setFrame:rect];
}
}
I am calling update method from cellForRowAtIndexPath.
self.accessibilityHint contain string like aa/bb/cc, aa/cc
UITableViewCell will layout subviews in layoutSubviews: after cellForRowAtIndexPath:, so your layout will be reset in cell's layoutSubviews.
You should move your layout code from cellForRowAtIndexPath: to cell's layoutSubviews.
Set label's autoresizingMask property, i always use it with cell's subviews.
I have a UIScrollView with a number of children UITextViews arranged vertically. I want the UIScrollView to resize to fit content. So my TextViews I do
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
[textView setNeedsLayout];
}
And for the scrollview I do
-(void)resizeScrollViewToFitContent
{
CGRect contentRect = CGRectZero;
for (UIView *view in self.scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
self.scrollView.contentSize = contentRect.size;
}
I call [self resizeScrollViewToFitContent] inside viewDidAppear. Any ideas why this setup is not working?
I am not sure how is the arrangement of your textViews inside the scrollView. I assume that they are arranged vertically right next to each other. I have created a sample project with a scrollView and 3 textViews and it works well with the following code:-
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGFloat height = self.textView1.frame.size.height +self.textView2.frame.size.height +self.textView3.frame.size.height ;
CGSize size = CGSizeMake(self.scrollView.frame.size.width, height);
self.scrollView.contentSize = size;
}
From my own project, the content Height of the scrollview is the combination of the height for all 3 textviews. It might be different in your project.
You shouldn't be doing the union of the contentFrame and view.frame. Try the following:
-(void)resizeScrollViewToFitContent
{
UIScrollView* scrollView;
CGSize contentSize = CGSizeZero;
for (UIView* subview in scrollView.subviews)
{
CGFloat subviewRight = CGRectGetMaxX(subview.frame);
if (subviewRight > contentSize.width)
{
contentSize.width = subviewRight;
}
CGFloat subviewBottom = CGRectGetMaxY(subview.frame);
if (subviewBottom > contentSize.height)
{
contentSize.height = subviewBottom;
}
}
scrollView.contentSize = contentSize;
}
One small thing worth noting: a UIScrollView's scroll indicators are subviews of any UIScrollView. This has the potential to throw off the above code's accuracy, but I've found that it always works except in some very particular cases.
If you know how many subviews are in your scrollview per row then you'll want to do a little math to find the correct height.
For example, you might have a uiview with a uiimageview and uilabel, so you're subview count will be 3 times what it should be.
try
// csv = contentscrollview
NSLog(#"%d", [[csv subviews] count] / 3);
I have a UIlabel that when a user clicks a button its text changes, this label is then resized using:
CGRect frame = label.frame;
[label sizeToFit];
frame.size.height = label.frame.size.height;
label.frame = frame;
So that the width is kept the same but the height of the label is changed so all text fits in this.
I then however need the uiview below this label to be moved down so that it starts at the bottom of the label, how do I do this?
Worked it out in the end by doing:
CGRect contentframe = _lbl_content.frame; //the uilabel
CGRect keyframe = _view_keystuff.frame; //the uiview
float startlocation = contentframe.origin.y;
startlocation += contentframe.size.height;
keyframe.origin.y = startlocation;
_view_keystuff.frame = keyframe;
You just want to put the UIView below the label?
Try this:
UIView *viewBelowLabel = [[UIView alloc] init];
viewBelowLabel.frame = CGRectMake( "your view origin x", label.frame.origin.y + label.frame.size.height, "your view size width", "your view size height");
You should adjust UIView frame after the resizing of the label
Using UIView's animateWithDuration:animations:completion:, I'm resizing a UIView and a subview of that UIView which is a subclass of UITableView.
The UIView resizes fine, but the UITableView doesn't. It does move around a little, but the frame does not update properly and reverts to its original state.
Edit: if I move the resizing to the completion block.... it works. What gives?
tweetTable.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[tweetTable endUpdates];
[UIView animateWithDuration:DURATION animations:^{
CGRect leftFrame = leftPane.frame;
leftFrame.size.width = self.view.frame.size.width - MARGIN;
leftPane.frame = leftFrame;
leftPaneButton.frame = leftFrame;
CGRect tweetFrame = tweetTable.frame;
tweetFrame.size.width = leftPane.frame.size.width;
NSLog(#"%f to %f", tweetTable.frame.size.width, leftPane.frame.size.width);
tweetTable.frame = tweetFrame;
tweetTable.backgroundColor = [UIColor redColor];
tweetTable.alpha = 0.5f;
sideInfo.center = CGPointMake(self.view.frame.size.width - MARGIN + (sideInfo.frame.size.width / 2), sideInfo.center.y);
rightPaneButton.center = sideInfo.center;
} completion:^(BOOL finished) {
leftExtended = TRUE;
[tweetTable beginUpdates];
}];
Check in your storyboard if the UIView has Autoresize Subviews checked. That means that it resizes all of its subviews when the view itself gets resized. That would explain why it works in the completion block.