UIButton设置iamge和 title后title不能显示的问题的问题

举个例子

我们首先设置一个,button他的大小是宽90, 高90

  
    UIButton *wcLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    wcLoginButton.backgroundColor = [UIColor redColor];
    wcLoginButton.frame = CGRectMake(CGRectGetMidX(sdkLoginView.frame)-45, 10, 90, 90);
   
    [wcLoginButton setImage:[UIImage imageNamed:@"XXX"] forState:(UIControlStateNormal)];
    wcLoginButton.imageView.backgroundColor = [UIColor greenColor];
   
    [wcLoginButton setTitle:@"微信" forState:(UIControlStateNormal)];
    wcLoginButton.titleLabel.backgroundColor = [UIColor whiteColor];
    wcLoginButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [wcLoginButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    wcLoginButton.titleLabel.font = [UIFont systemFontOfSize:13];
   
   
// button标题的偏移量

    wcLoginButton.titleEdgeInsets = UIEdgeInsetsMake(wcLoginButton.imageView.frame.size.height+5, -wcLoginButton.imageView.bounds.size.width, 0,0);

    // button图片的偏移量

    wcLoginButton.imageEdgeInsets = UIEdgeInsetsMake(0, wcLoginButton.titleLabel.frame.size.width/2, wcLoginButton.titleLabel.frame.size.height+5, - wcLoginButton.titleLabel.frame.size.width/2);

  [self.view  addSubview:wcLoginButton];

以上仅代表个人观点,是个人在实际工作中实验出来的,如有不妥之处,各位大神指出。谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。