In the previous development process, encountered such a small problem: To button set a picture, the picture is gray, the artwork does not have a problem, the method to set the button is also very simple, a code to do, but run the time but found that the picture has become blue for no reason. Later I studied the discovery, in some controls to set the picture will default to the image rendered blue, to find the problem, it is good ending, the solution is as follows:
UIButton *loginbtn = [[UIButton alloc]initwithframe:cgrectmake (+, +, +)]; loginbtn.showstouchwhenhighlighted = YES; UIImage *loginimg = [UIImage imagenamed:@ "Login"]; loginimg = [loginimg imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; [Loginbtn setimage:loginimg forstate:uicontrolstatenormal]; [Self.view ADDSUBVIEW:LOGINBTN];
-(UIImage *) Imagewithrenderingmode: (uiimagerenderingmode) renderingmode
This method is used to set the rendering mode of the image.
Uiimagerenderingmodealwaysoriginal This enumeration value is to declare that the picture is to be displayed as it was originally, and that it does not need to be rendered into another color
Ok! The problem ends. Put this little problem here to prevent the next encounter and forget how to solve!
Incidentally, if your artwork doesn't give you the button's size, just gives you the button's picture, then you can directly take the image size, then set the button to the same size as the picture
UIButton *loginbtn = [UIButton buttonwithtype:uibuttontypecustom]; loginbtn.showstouchwhenhighlighted = YES; UIImage *loginimg = [UIImage imagenamed:@ "Login"]; loginimg = [loginimg imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; [Loginbtn setimage:loginimg forstate:uicontrolstatenormal]; Cgsize size = loginbtn.currentimage.size;//The dimension of the image taken to the button Loginbtn.frame = CGRectMake (+, Size.width, Size.Height); [Self.view ADDSUBVIEW:LOGINBTN];
ios-How to change a picture into blue