UIButton在iPad OS 7.0上缩小

时间:2014-05-14 15:41:25

标签: ios objective-c ipad uibutton uiimage

我遇到了自定义UIButton Image和我的iPad 2/3的问题:

问题

我从尺寸为72 x 72像素的图像创建一个按钮。我将按钮框设置为相同尺寸的图像。当我在iOS模拟器中运行代码时,会出现一个72 x 72 px按钮(缩放为50%)。但是,当我在iPad上运行代码时,按钮的尺寸从72 x 72像素缩小到大约45 x 45像素?当我拍摄iPad屏幕截图时,缩放到50%时会显示72 x 72像素的正确尺寸我需要iPad上的按钮为72 x 72像素,如何修复?还是可以修好?是否由于设备的大小而自动缩放图像?

修复尝试

  1. 我添加了2x图像以容纳视网膜,使图像更大但不是72 x 72
  2. 我将图像的分辨率从300 dpi更改为72 dpi且无变化。
  3. 看了这个解决方案:UIButton Shrinking Image。我的图片有一个alpha通道,所以没有修复
  4. 看了这个解决方案:custom UIButton image is getting reduced (shrinked) in iphone OS 4.0。据我所知,我的图像没有任何空白边框
  5. 材料

    运行iOS 7.0 / iPad 2的iPad 3(视网膜)运行ios 7.1.1

    图片:RedTarget.png / RedTarget@2x.png

    • 尺寸:72 x 72像素/ 144 x 144像素
    • 分辨率:299像素/英寸
    • Alpha频道:是
    • 色彩空间:RGB

    使用简单代码

    UIImage *img;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        img = [UIImage imageNamed:@"RedTarget@2x.png"];
    }
    else
    {
        img = [UIImage imageNamed:@"RedTarget.png"];
    
    }
    
    CGFloat h =  img.size.height;
    CGFloat w =  img.size.width;
    
    UIButton* targetbutton = [[UIButton alloc]initWithFrame:CGRectMake(512,384, w, h)];
    
    NSLog(@"%f %f", w,  h);
    
    [targetbutton setImage:img forState:UIControlStateNormal];
    
    targetbutton.center = CGPointMake(512,384);   
    
    NSLog(@"%@", targetbutton);
    
    [self.view addSubview:targetbutton];
    

    当我在iPad 3上运行此代码时,NSLog语句将打印如下:

    2014-05-14 10:19:40.511 TestSizeButton[574:60b] 144.000000 144.000000
    2014-05-14 10:19:40.515 TestSizeButton[574:60b] <UIButton: 0x16d495a0; frame = (440 312; 144 144); opaque = NO; layer = <CALayer: 0x16d49730>>
    

    提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试在viewWillAppear或viewDidAppear方法中添加按钮,并检查它是否进一步缩小。还要在装载时检查视图位置是否正在移动。如果您的视图正在转移,请在viewDidLoad方法中添加它

if( [self respondsToSelector:@selector(setEdgesForExtendedLayout:)] )
{
    self.edgesForExtendedLayout=UIRectEdgeNone;
}