2013-06-26

Strange Behavior: UIButton Text will be not shown.

Because of a multilingual project, all the texts should be translated and replaced with the correct language version. Everything was fine until I found that a button won't show the text assigned.

The code:


 [button titleLabel].text = @"I won't be changed";


The result is: the button shows always the text that is set up in the storyboard. If I delete the text in Storyboard, it will even disappear when I tap!

The solution is also simple:


[button setTitle:@"I will change" forState:UIControlStateNormal]


via StackOverflow

2013-06-17

One Class for One Problem

The keyboard on iOS devices is sometimes a nightmare for a programmer.  Take the UITextField as an example, when a keyboard appears, it will cover most screen area and let the text fields hidden in the back of the soft keyboard. A solution provided by Apple is using the UIScrollView, but there are a lot of mathematical calculation for a programmer. :(

After a while research on the internet, I've found that Michael Tyson has built a drop-in solution for such kind of problems. He packs everything that one needs into a class and puts the sources on GitHub.

If you follow the instruction on the Github, the task with UITextField and Keyboard is easy as a cake!

Usage

For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it.
For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class toTPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.


Well done, Michael!

via ATasty Pixel

HOWTO: UIScrollView scroll to bottom programmatically

The autolayout function of a view controller is very useful. The UI elements can find their position after a rotation automatically. Except for UIScollerview...Orz

The reason is: The Layout in Storyboard is normally designed for portrait, not for landscape. So if an user rotates his iOS devices, the UIScrollView cannot resize its content height automatically. So a programmatical resize is needed. BUT, there is another way: just scroll the UIScrollView to the bottom(or where you want), the content will be presented in landscape correctly like in portait.

Here is the solution: Adding the following code to the view controller where the scroll view is.



-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// Scroll to the bottom
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];
}

via StackOverflow