UITableView footer view issue in iOS 10 due to autoresizingMask - ios

I am using the following code for adding table footer view with a label.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
view.autoresizingMask = self.view.autoresizingMask;
UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
label.text = #"The End";
label.textAlignment = NSTextAlignmentCenter;
[view addSubview:label];
self.tableView.tableFooterView = view;
It was working fine with iOS version less than iOS 10.
Only on iOS 10 I footer view goes outside of the frame and is getting displayed only if we pull up the tableview out of its scrolling bounds.
And on releasing the tableview again the footer view goes out of view.
Can anyone please let me know whats happening here in iOS 10, Do we need to change the approach shown above.
But I found that by removing the following line of code it works fine.
view.autoresizingMask = self.view.autoresizingMask;
Could anyone explain why this happens ?

Related

UITableViewCell separator insets don't work when table.width is much greater than view.width

UITableViewCell separator insets don't work when table.width is much greater than view.width on iOS 10, I haven't try it on the device which iOS version is less then iOS 10.
code is here on gist
You can add separator in your UITableView using the below code
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor whiteColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
and remove the default separator of your UITableView. Hope it helps.
_tableView.cellLayoutMarginsFollowReadableWidth = NO;
it works above.
refer to
iOS 9 UITableView separators insets (significant left margin)
Xcode 7 iOS 9 UITableViewCell Separator Inset issue
There are two ways in which you can solve this problem.
First one you can add a separator line view in cell content view.
UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableview.frame.size.width, 3)];
line.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:line];
The second one is apple provided one and pretty easy also just add this line of code where u declare your tableview
yourTableView.cellLayoutMarginsFollowReadableWidth = NO;

How can I get the text to center in my scrollable UILabel?

The scrollable label works fine, but for some reason I cant get the text in the UILabel to center in the UIScrollView, it always stays left aligned. Here is an image of what I am talking about, and my code. (P.S. the UILabel is a subview of the UIScrollView, and the UIScrollView is a subview of the UITableViewCell.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(cell.frame.size.width/21.5, 25, cell.frame.size.width - 40 , 20)];
scrollView.showsHorizontalScrollIndicator = NO;
[cell addSubview:scrollView];
scrollLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 20)];
scrollLabel.text = secondaryLabel.text;
scrollLabel.font = secondaryLabel.font;
scrollLabel.backgroundColor = [UIColor clearColor];
scrollLabel.textColor = [UIColor whiteColor];
scrollLabel.numberOfLines = 1;
scrollLabel.textAlignment = NSTextAlignmentCenter;
[scrollLabel sizeToFit];
[scrollView addSubview:scrollLabel];
scrollView.contentSize = CGSizeMake(scrollLabel.frame.size.width, 20);
I think the code you written for UILabel is pretty much right and the center TextAlignment is there but the problem is you need to pass the right X & Y for UILabel Frame's. For now they are getting set to 0-0 and are adding on the Scrollview's starting frame. This might can solve your problem.

Scroll UILabel with UITableView

UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = CONNECTED;
messageLabel.textColor = [UIColor blackColor];
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.TableView.backgroundView = messageLabel;
The last row of this code set backgroundView to my UILabel but it stays centered and if I scroll the table view, the message position stays fixed. How to solve this problem?
I want that the UILabel to follow the scroll event.
Just add your UILabel as a subview to your tableView and send it back.
[self.TableView addSubview:messageLabel];
[self.TableView sendSubviewToBack:messageLabel];
Now when you scroll the tableView, this view will also get scrolled.
Other code enhancements (Not related to the question)
You can initialize your label like this
UILabel *messageLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
You can see the following tutorials of TableView:
swift: https://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/
objective-c: http://www.makemegeek.com/uitableview-example-ios/
Basically,you should add your label inside a cell and it will work.

UILabel will draw something with Chinese even if the width is equal to 0 in iOS 8

Just creat a new "Single View Application" and add the code to the viewDidLoad method, you will see it:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 0, 100)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor blackColor];
label.text = #"中文";
[self.view addSubview:label];
After viewDidLoad() is called and before the UILabel shows up on the screen,
iOS does multiple steps.
Some of them are
Calling viewWillLayoutSubviews
Calculating the layout
Calling viewDidLayoutSubviews
Calling viewWillAppear and finally
Calling viewDidAppear
Most likely your view was resized when the layout was done, please check in both, viewDidLayoutSubviews and viewDidAppear what your frame size is then.
i think you need to add below code
label.clipsToBounds = YES;
It's just an iOS bug, has been fixed in iOS 10, but still appear in the iOS 8.1.
In my case, setHidden:YES instead of setWidth:0 will solve this problem.

Programmatically created UILabel not working in iOS 6

I programmatically create a few UILabels in my app. In iOS 5 this code worked fine.
In iOS 6 the labels simply don't seem to appear on the screen.
I don't see any warnings, the code definitely executes, but no label.
I've tried searching for similar issues but can't find anything. My labels have literally just disappeared.
Any help appreciated.
float popUpWidth = 700.0
//Add label
UILabel *label;
label = [[UILabel new] initWithFrame:CGRectMake(0,0,popUpWidth,50)];
label.text = #"Notes";
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor blackColor];
label.textColor = [UIColor whiteColor];
//Add label to view
[self.view addSubview:label];
The problem is you are creating a label twice as wide as the devices screen, and setting a small amount of text to be centered thus causing the text to be off screen. Remember CGRect uses points not pixels. Additionally, I tested your code and had to replace "new" with "alloc" for it to work properly, I suggest you do the same.
The full correct line would be:
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,popUpWidth,50)];
Calling [UILabel new] is like calling [[UILabel alloc] init] so you should change your code like this:
label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,popUpWidth,50)];

Resources