iphone - 具有固定大小自定义图像的 UIButton

标签 iphone objective-c ios ipad uibutton

我有一个自定义的 UIButton,如下所示:

 UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
        [action setSize:CGSizeMake(30, 30)];
    [action setBackgroundImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
    [action setContentMode:UIViewContentModeCenter];
    [action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

我拥有的真实图像实际上大小为 10x10,居中,我希望它保持这种状态,而不是缩放到按钮大小。我该怎么做?

修改后的代码:

UIButton * action = [UIButton buttonWithType:UIButtonTypeCustom];
    [action setSize:CGSizeMake(44, 44)];
    [action setImage:[UIImage imageNamed:@"contextaction.png"] forState:UIControlStateNormal];
    [action addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    if (IS_IPAD){
        action.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        action.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }
     actionButton_ = [[UIBarButtonItem alloc] initWithCustomView:action];

仍在扩展

最佳答案

为此,我认为您需要使用图像插入属性 UIButton Class Reference

关于iphone - 具有固定大小自定义图像的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711437/

相关文章:

iphone - nsfetchedresultscontroller 中部分名称键路径的无关系值

iphone - 启动应用程序时如何读取应用程序设置(从 iPhone 设置应用程序)

objective-c - 如何检测网络断开?

ios - 自定义 NSURLProtocol 与 NSURLSession

ios - 使用大量 bean 更快地初始化 Typhoon 工厂

iphone - 此应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃

iphone - 如何从应用程序内监控网络事件?

ios - GoogleMaps SDK 中不存在 Google Places 自动完成功能

iOS 无法从 iTunes 获取准确的应用程序版本

ios - sizeToFit 依赖 layoutSubviews 如何实现?