您的位置:首页 > 移动开发 > IOS开发

iOS开发中遇到的一些问题以及解决办法总结

2014-05-12 14:09 1026 查看
(1)设置UIButton选中状态下的标题颜色,和背景颜色:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(10, 100, 100, 50)];
[btn setTitle:@"推荐" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[btn setBackgroundImage:[UIImage imageNamed:@"首页中间黑条.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"首页选中.png"] forState:UIControlStateHighlighted];


(2)在ios7下导航条背景图片高度如果大于44, 默认是覆盖状态栏的;如果高度等于44,则不会覆盖状态栏,但是会出现一个问题:如果存在UITabBarController,在点击UITabBarItem切换页面的时候,状态栏是消失状态,再次点击这个标签,状态栏又会出现.

解决方法是:在每一个viewController中添加
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;


(3)如果某个 UIViewController的第一个子视图是
UIScollView或者UIScollView的子类(UITableView和UITextView),同时当这个
UIViewController 被 push
或 initWithRootController成为
UINavigationController控制的Controller时,这UIViewController的
view 的子视图 UIScollView
的所有子视图,
都会被下移 64px。

解决办法:
  <1>在 ViewController的
init的方法中增加一行代  
码:self.automaticallyAdjustsScrollViewInsets = NO;
  <2>让UIScrollView不要成为 ViewController
的 View
的第一个子视图,在viewDidLoad方法   
                   的第一行中, 加入 UIView *nullView = [[UIViewalloc] init];
[self.view addSubview:nullView];
  <3>将 UIScorllView的子视图上移 64.0px


(4)设置UIButton的title字体.用[btn.titleLabel setFont:[UIFontsystemFontOfSize:14]];

(5)UIImage的ImageNamed:forState:方法会把图片放进缓存,使用时应当注意

(6)如果项目中存在a.png和a@2x.png两张图片,使用代码:

[btnsetImage:[UIImageimageNamed:@"a.png"] forState:UIControlStateNormal];
     <1>在retina显示屏下自动加载a@2x.png图片,并把图片按照原比例, 宽高各缩小2倍;
    <2>在普通屏下自动加载a.png图片,宽高不会缩小;

(7)如果项目中只存在a.png一张图片,使用代码:

[btnsetImage:[UIImageimageNamed:@"a.png"] forState:UIControlStateNormal];在retina屏和普通屏下都会按照图片的原比例显示.

(8)如果项目中只存在a@2x.png一张图片,使用代码:

[btnsetImage:[UIImageimageNamed:@"a.png"] forState:UIControlStateNormal];在retina屏下会显示, 在普通屏下不会显示.

(9)UITabBarController的标签UITabBarItem在选中的情况下默认图片和文字都会变成蓝色(Blue).
<1>使用UIImage的方法imageWithRenderingMode:(UIImageRenderingMode)renderingMode,
UIImageRenderingMode选UIImageRenderingModeAlwaysOriginal会使图片恢复本来的样子
<2>使用UITabBar的tintColor:方法可以改变文字颜色

(10)如果一个UIViewController被push进入了一个UINavigationController,那么这个
           UIViewController的状态栏的样式是根据UINavigationController的navigationBar

   
的样式改变的,复写继承自UIViewController的preferredStatusBarStyle方法不会执行, 例如:

<1>如果self.navigationController.navigationBar.barStyle
= UIBarStyleBlack;那么此时这个

UIViewController中的状态栏的样式为UIStatusBarStyleLightContent(也就是黑底白字).

<2>如果self.navigationController.navigationBar.barStyle
= UIBarStyleDefault;那么此时这个

UIViewController中的状态栏的样式为UIStatusBarStyleDefault(也就是白底黑字).

(11)如果一个UIViewController没有被push进一个UINavigationController,那么复写继承自UIViewController的preferredStatusBarStyle方法会执行,可以改变状态栏的颜色.
- (UIStatusBarStyle)preferredStatusBarStyle{
NSLog(@"%@",NSStringFromSelector(_cmd));
returnUIStatusBarStyleDefault;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息