IOS新手,在开发中遇到的一些细节问题,总结一下,以免后来用到。
总结一下控件。
一、UITableview1、分割线左边短一块
系统自带的控件的分隔符是左边短一块的,详见短信的界面。为了使其与屏幕一边长,需要加入如下代码
(1)如果该界面是UITabViewController,则需加入
- (void)viewDidLayoutSubviews
{
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
}
}
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
(2)如果只是UITableView控件,先在viewDidLoad中加入
if ([_table respondsToSelector:@selector(setSeparatorInset:)]) {
[_table setSeparatorInset:UIEdgeInsetsZero];
}
if ([_table respondsToSelector:@selector(setLayoutMargins:)]) {
[_table setLayoutMargins:UIEdgeInsetsZero];
}
然后
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
2、去掉多余的项
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor clearColor];
[_table setTableFooterView:view];
[_table setTableHeaderView:view];
3、下拉刷新
[_table addSubview:UIRefreshControl];//针对单独的UITableView控件,UITableViewController有一个RefreshControl的私有成员。
二、UILabel
1、自动换行的Label
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
_attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0], NSParagraphStyleAttributeName:paragraphStyle};
CGSize sizedate = [_label.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width-16, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:_attributes context:nil].size;
CGRect rectdate =_label.frame;
rectdate.size.height = sizedate.height;
_label.frame = rectdate;
然后再将这个Size赋给Label;
三、UINavigationController
1、可以通过viewControllers访问其所有View,根节点为0;
四、控件布局
1、可以用setFrame来做,Frame是相对于父控件的坐标参数,而bounds是相对于界面的。