【问题标题】:Changing EdgeInsets of Image and Title of UIButton makes the button looks blur更改 UIButton 的 Image 和 Title 的 EdgeInsets 使按钮看起来模糊
【发布时间】:2017-07-01 11:58:19
【问题描述】:

我正在以编程方式创建 UIButton,当我设置按钮标题和图像的 EdgeInsets 时,按钮标题和图像变得模糊。 (见参考截图) 为什么标题和图像会模糊? 这是我的代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
           action:@selector(buttonPressed:)
 forControlEvents:UIControlEventTouchUpInside];

CGRect buttonFrame = CGRectMake(0.0, 0.0, mainFrame.size.width, mainFrame.size.height);
button.frame = buttonFrame;
[button setTitle:@"Press me" forState:UIControlStateNormal];
[button setTitle:@"Pressed" forState:UIControlStateSelected];
[button.titleLabel setFont:[UIFont fontWithName:FNT_ALL_TEXTS size:10]];

[button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imageSelected] forState:UIControlStateSelected];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage * refimage = [UIImage imageNamed:imageNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(buttonFrame.size.height*0.5, -refimage.size.width*1.0, 0.0f, 0.0f)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, buttonFrame.size.height*0.35, 0.0)];
[self addSubview:button];

【问题讨论】:

    标签: ios objective-c uibutton blur stretch


    【解决方案1】:

    亲爱的,只要确保它是按钮的内容垂直和水平对齐,但是你在按钮中使用图像或文本。

    UIButton *btn;
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    

    // 水平对齐

    UIControlContentHorizontalAlignmentLeft
    UIControlContentHorizontalAlignmentRight
    UIControlContentHorizontalAlignmentFill
    UIControlContentHorizontalAlignmentCenter
    
    // Vertical Alignment
    
    UIControlContentVerticalAlignmentTop
    UIControlContentVerticalAlignmentFill
    UIControlContentVerticalAlignmentBottom
    UIControlContentVerticalAlignmentCenter
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      相关资源
      最近更新 更多