UILabel Position not change inside UITableviewcell - ios

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.

Related

IOS uiview frame didn't update

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

How to increase the tap area of UIButton

I am using the following code to resize the buttons in my appplication
+(void) processButton:(UIButton*) button buttonType:(NSString*)buttonType {
ENDebug(#"Process button %#" ,[button restorationIdentifier]);
[button setTranslatesAutoresizingMaskIntoConstraints:true];
CGRect rect = [button frame];
float startx = rect.origin.x;
float midx = startx +rect.size.width/2;
float starty = rect.origin.y;
float midy = starty +rect.size.height/2;
if ([buttonType isEqualToString:BUTTON_IPAD_SMALL]||[buttonType isEqualToString:BUTTON_IPHONE_SMALL]) {
rect.size.width = BUTTON_WIDTH_SMALL;
rect.size.height = BUTTON_HEIGHT_SMALL;
} else {
rect.size.width = BUTTON_WIDTH_LARGE;
rect.size.height = BUTTON_HEIGHT_LARGE;
}
rect.origin.x = midx - rect.size.width/2;
rect.origin.y = midy-rect.size.height/2;
button.titleLabel.font = GillSansBold(BUTTON_TITLE_SIZE);
[button setFrame:rect];
}
Using this I am able to resize the code.. but am not able to increase the tap size of the buttons.. Looks like only the view has expanded without the button becoming clickable in entirety.
Check the edge insets of the button. An inset is a margin around the drawing rectangle. Or you cant just try:
button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
after setting the button's frame.

how to resize UIScrollView to fit content of children UITextViews

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);

custom uitableviewcell reset subview frame doesn't work as I wanted

I custom a uitableviewcell with nib, and want to custom the highlighted style. When the cell highlighted, I want to reduce the size of the subview of the cell and keep the position of the imageview in the cell not changing. So, it looks like the frame of a imageview zoomed out, but the image itself stay there not changing its position.
However, this code snippet doesn't work as I wanted.Could anybody help me to figure out where I am wrong. Any help will be appreciated!
tips: the imageview is a subview of self.frameView and frameView is a subview of frameBgView.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted)
{
self.frameView.backgroundColor = UIColorFromRGBA(0, 0, 0, 0.1);
CGRect rect = self.frameBgView.frame;
rect.origin.x += 10;
rect.size.width -= 20;
self.frameBgView.frame = rect;
rect = self.frameView.frame;
rect.origin.x -= 10;
self.frameView.frame = rect;
}
else
{
....
}
}
EDIT: some screenshots to explain the question:
oky i tried your solution but i got like this i am posting the code as well as the output how it looks, if there is any problem just comment , i am deleted the frameBgView it is not required
code
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted)
{
self.frameView.backgroundColor = [UIColor grayColor];
CGRect rect = self.contentView.bounds;
rect.origin.x += 15;
rect.size.width -= 30;
//rect = self.frameView.frame;
// rect.origin.x -= 10;
self.frameView.frame = rect;
}
else
{
self.frameView.backgroundColor = [UIColor whiteColor];
CGRect rect = self.contentView.bounds;
rect.origin.x += 10;
rect.size.width -= 20;
self.frameView.frame = rect;
}
}
and i am not using auto layout .. the result what i got is, before
and after highlighted,
EDIT:
this is the cell structure
as u can see in the picture, in content view i hav added frameView which is blue in colour, and within frameView i hav added imageView and also don't forget t set content mode scale to fill and also auto resizing masks for both frameView and imageView for example
autoresizing masks for content view
autoresizing masks for frameView
autoresizing masks for imageView
END EDIT

contentOffset Changes when Setting UITextView Frame in textViewDidChange

I am trying to set the height of a UITextView dynamically in textViewDidChange. I get the frame to adjust to the proper height, but when the height is greater than a threshold that I have set, then the textView should just scroll with a new line. However, the contentOffset gets messed up and it doesn't scroll down to fully include the new line.
- (void)viewDidLoad
{
[super viewDidLoad];
_textView.frame = CGRectMake(60, 100, 200, _textView.contentSize.height);
_textView.scrollEnabled = NO;
}
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(#"Offset: %f", _textView.contentOffset.y);
CGFloat height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
if (_textView.contentSize.height >= 120) {
height = 120;
_textView.scrollEnabled = YES;
_textView.contentOffset = CGPointMake(0, _textView.contentSize.height - _textView.frame.size.height);
}
CGRect frame = _textView.frame;
frame.size.height = height;
_textView.frame = frame;
}
I suppose an easier way of explaining what is happening is this. On a completely blank view controller add a UITextView in Storyboard. Then add only these three lines into textViewDidChange:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
Every other time the textView's height increases, the text is not vertically centered. If you scroll it around though and then let go it will return to the correct position. The frame is always the correct size. It is the contentOffset that is not quite right.
I wonder if you could post some code as to what you're actually doing?
I've just tried this on 5.0 and this works.
-(void) textViewDidChange:(UITextView *)textView {
int maxHeight = 30;
CGFloat height = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping].height;
CGRect rect = textView.frame;
rect.size.height = MIN(height, maxHeight);
[textView setFrame:rect];
}

Resources