控件使用注意事项

1.UISegmentedControl

    

1,UISegmentedControl是通过selectedSegmentIndex选择的

2,- (IBAction)segmentedTouch:(id)sender {

    UISegmentedControl *sm = (UISegmentedControl *)sender;
    switch (sm.selectedSegmentIndex)
     {
        case 0:
            self.rightSwitch.hidden = YES;
            self.leftSwitch.hidden = NO;
            break;
        case 1:
            self.rightSwitch.hidden = NO;
            self.leftSwitch.hidden = YES;
            break;
            
        default:
            break;
     }
}


2.UIButton

    1,button属性Selected
    - (IBActin)download:(UIButton *)sender
    {
        sender.selected = !sender.isSelected;
    }
    
    2.button多图标点击切换背景Disabled
    - (void)btnClick:(UIButton *)btn
    {
   // 设置点击按钮切换背景,上一次点击的图标恢复
    self.selectBtn.enabled = YES;
    btn.enabled = NO;
    self.selectBtn = btn;
    }
    
    2,//设置titile的位置
    [but setTitleEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
    //设置背景图片,tiile在image上方
     [but setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlState];
    
    3,高亮选择,切断交互
    方法一:storyBoard->Button->Drawing->Highligted Adjuest Image
    方法二:storyBoard->Button->view->Interaction->User Interaction Enabled
    
    4,设置UIButton或者UILable下划线
     NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] 
                                             initWithString:@"找回密码"];
    [titleString addAttribute:NSUnderlineStyleAttributeName value
                             :NSNumber numberWithInteger:NSUnderlineStyleSingle] 
                             range:NSMakeRange(0, [titleString length])];
    [button setAttributedTitle: titleString forState:UIControlStateNormal];
    
    5.禁止高亮或disabel
    1)重写setHeight、disabel方法
    2)self.highlighted = NO
    3) self.adjustsImageWhenHighlighted = NO; //禁止高亮
       self.adjustsImageWhenDisabled = NO;
    
    6.获得btn标题
    NSString *titile = btn.currentTitle;


3.UILable

1.自动换行
labelTxt.numberOfLines = 0;

2.设置text样式
    UILabel *textView = [[UILabel alloc] init];
    textView.numberOfLines = 0;
    NSString *str = [NSString stringWithFormat:@"%@\n%@",prefix,name];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
    //range:NSMakeRange(location, led) 
    //ps:NSStrokeWidthAttributeName表示文字的空心
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:[str rangeOfString:prefix]];
    //设置label的text
    textView.attributedText = attr;
    
3.计算自适应label的高度
    CGSize labelSize;
    int newMarinW;
    int oldMarinW;
    NSInteger modelScreen = [UIDevice currentResolution];

    switch (modelScreen) {
        case 4:
            newMarinW = 200;
            oldMarinW = 20;
            break;
        case 3:
            newMarinW = 180;
            oldMarinW = 35;
            break;
        case 2:
            newMarinW = 160;
            oldMarinW = 70;
            break;
        default:
            newMarinW = 160;
            oldMarinW = 70;
            break;
    }  
   //判断手机版本
    NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];
    if ([strSysVersion doubleValue] <= 8.0) {
        //iOS7以下的方法
         labelSize = [self.content.text sizeWithFont:self.content.font constrainedToSize:CGSizeMake(self.content.width-oldMarinW, MAXFLOAT) lineBreakMode:NSLineBreakByClipping];
    }else{
        //iOS8以上的方法
        NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName:[UIColor blackColor]};
        labelSize = [self.content.text boundingRectWithSize:CGSizeMake(newMarinW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dict context:nil].size;
    }
    self.content.height = labelSize.height;


4.UIImageView

1,图片适配填充
    
        /**
         UIViewContentModeScaleToFill : 图片拉伸至填充整个UIImageView(图片可能会变形)
         UIViewContentModeScaleAspectFit : 图片拉伸至完全显示在UIImageView里面为止(图片不会变形)
         UIViewContentModeScaleAspectFill :图片拉伸至 图片的宽度等于UIImageView的宽度 或者 图片的高度等于UIImageView的高度 为止
         
         UIViewContentModeRedraw : 调用了setNeedsDisplay方法时,就会将图片重新渲染
         
         UIViewContentModeCenter : 不会拉伸,图片直接将中间的部分放入imageView显示
         UIViewContentModeTop,
         UIViewContentModeBottom,
         UIViewContentModeLeft,
         UIViewContentModeRight,
         UIViewContentModeTopLeft,
         UIViewContentModeTopRight,
         UIViewContentModeBottomLeft,
         UIViewContentModeBottomRight,
         
         经验规律:
         1.凡是带有Scale单词的,图片都会拉伸
         2.凡是带有Aspect单词的,图片都会保持原来的宽高比,图片不会变形
         */
#waringning 使图片等比例拉伸和超出边框的内容都剪掉      
        // 内容模式
        self.contentMode = UIViewContentModeScaleAspectFill;
        // 超出边框的内容都剪掉
        self.clipsToBounds = YES;


5.UITextView

1 判断textView或textField是否有text
BOOL isValue = self.textView.hastext


6.UIActionSheet

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
    //必须要使用showInView
    [sheet showInView:insuranceUpload];


7.UIScrollView

1. scrollView.pagingEnabled = YES; //可以分页滑动

2 除去垂直和水平方向的滚动条,则可以除去scrolView多余的subview
        scrollView.showsHorizontalScrollIndicator = NO;//去除水平方法滚动条
        scrollView.showsVerticalScrollIndicator = YES; //去除垂直方向滚动条


8.UIPageControl

1. pageControl.userInteractionEnabled = NO;

2.使用kvc改变pageControl的私有方法,设置内部圆点图片
        [pageControl setValue:[UIImage imageNamed:]  forKey:@"pageImage"];
        [pageControl setValue:[UIImage imageNamed:] forKey:@"currentPageImage"];

3.禁止原点点击可切换
pageControl.userInteractionEnabled = NO;

4.换页
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double pageNo = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage = (int)(pageNo + 0.5);
}



你可能感兴趣的:(UIButton)