Implementing iPad General Setting Page - ios

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

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.

UITableViewAutomaticDimension on custom UITableViewCell

I am using UITableViewAutomaticDimension for multiline UILabel in UITableViewCell and everything works fine on iPhone. To add a margin to the table view on iPad I'm using a custom subclass overriding setFrame:
- (void)setFrame:(CGRect)frame {
if (IS_IPAD) {
CGFloat inset = 100;
frame.origin.x += inset;
frame.size.width -= 2 * inset;
}
[super setFrame:frame];
}
The problem is, that the UITableViewAutomaticDimension is returning the wrong (too small) height for the cell on iPad and the label gets cut off. I assume it's returning the height calculated for the full width.
Is there any way to get the table view calculate the right height?
EDIT:
found a quite hacky solution, but this does not work when setting attributedText
- (void)setFrame:(CGRect)frame {
if (IS_IPAD) {
CGFloat inset = 100;
frame.origin.x += inset;
frame.size.width -= 2 * inset;
for (UIView *subview in self.contentView.subviews) {
if ([subview isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)subview;
if (label.numberOfLines == 0) {
label.preferredMaxLayoutWidth = frame.size.width;
}
}
}
}
[super setFrame:frame];
}
If you are doing something like this. Combining technology of Size Classes and Auto-Layout is able to solve the problem in a very easy way. Otherwise, it will require a lot of redrawing and calculation when you want to do that in a manual way. Also, when you are using UITableViewAutomaticDimension, you should go with this way as well because this flag simply indicates that iOS should calculate the cell height automatically via Auto-Layout.

Increase UIView size not working

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.

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