setBackgroundImage和setImage

来源:互联网 发布:数据库not null 编辑:程序博客网 时间:2024/06/05 17:37

setBackgroundImage和setImage

这两个还是有区别的

例如:


当你需要设置按钮的frame为当前图片的frame时候,如果用setImage,那么frame为nil  这时,只能用setBackgroundImage 才能使得按钮的frame为image的frame


        UIButton * backBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        [backBtn setImage:[UIImageimageNamed:@"---"]forState:UIControlStateNormal];

        CGRect tempFrame = backBtn.frame;

        tempFrame.size = backBtn.currentBackgroundImage.size;

        backBtn.frame = tempFrame;


此时,backBtn的frame 为nil



        UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [backBtn setBackgroundImage:[UIImage imageNamed:@"---"forState:UIControlStateNormal];

         CGRect tempFrame = backBtn.frame;

        tempFrame.size = backBtn.currentBackgroundImage.size;

        backBtn.frame = tempFrame;


此时,backBtn的frame 为image的frame

0 0
原创粉丝点击