iOS:使图像宽度略小于屏幕宽度

时间:2015-08-29 14:52:44

标签: ios objective-c

我的问题是我的图片太大了。我需要它略小于屏幕宽度。

这是我的控制人员:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageName = @"goldencoaster";

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table"]]];

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.imageName]];
    CGRect bounds;
    bounds.origin = CGPointZero;
    bounds.size = [[UIScreen mainScreen] bounds].size;

    self.coasterImage.bounds = bounds;
    self.coasterImage.image = image;
}

但是,当我运行模拟器时会返回此信息:

enter image description here

如何让图像显示略小于屏幕宽度?

=============================================== ===================== 的更新

所以我按照建议取出了自动布局,并将我的控制器更新为:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageName = @"goldencoaster";

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table"]]];

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.imageName]];

    CGRect screen = [[UIScreen mainScreen] bounds];

    CGRect bounds;
    bounds.origin.x = screen.origin.x + 10;
    bounds.origin.y = screen.origin.y + 10;
    bounds.size.width = screen.size.width - 100;

    self.coasterImage.frame = bounds;
    self.coasterImage.image = image;
}

然而,现在我的形象根本没有出现?只显示背景(名称为#34;图像为#34;的图像),但不显示黄金过滤器。

4 个答案:

答案 0 :(得分:3)

试试这个

- (void)viewDidLoad {
[super viewDidLoad];

self.imageName = @"goldencoaster";

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table"]]];

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.imageName]];
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = [[UIScreen mainScreen] bounds].size;

self.coasterImage.bounds = bounds;
self.coasterImage.image = image;
self.coasterImage.frame = CGRectMake(20, 0,   self.view.frame.size.width-40, self.view.frame.size.height);
self.coasterImage.center = self.coasterImage.superview.center;

}

Swift 3.1

override func viewDidLoad() {
    super.viewDidLoad()

    imageName = "goldencoaster"

    view.backgroundColor = UIColor(patternImage: UIImage(named: "table")!)

    let image = UIImage(named: imageName)
    var bounds = CGRect()
    bounds.origin = CGPoint.zero
    bounds.size = (UIScreen.main.bounds).size

    coasterImage.bounds = bounds
    coasterImage.image = image
    coasterImage.frame = CGRect(x: CGFloat(20), y: 0, width: self.view.frame.size.width - CGFloat(40), height: self.view.frame.size.height)
    coasterImage.center = (coasterImage.superview?.center)!
}

答案 1 :(得分:2)

您可以使用contentMode(如

)来完成此操作
self.coasterImage.contentMode = UIViewContentModeScaleAspectFit;

这里是不同的contentMode从这里检查

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit      // contents scaled to fit with fixed aspect. remainder is transparent


UIViewContentModeScaleAspectFill     // contents scaled to fill with fixed aspect. some portion of content may be clipped.

UIViewContentModeRedraw              // redraw on bounds change (calls -setNeedsDisplay)

UIViewContentModeCenter              // contents remain same size.    positioned adjusted.

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

答案 2 :(得分:1)

两个字:自动布局

You want your UI to adapt to different devices, and orientations.

Auto Layout和尺寸类旨在满足这些需求。

<强>更新

您正在检查屏幕尺寸和设置边界。这表明您正在尝试调整视图大小,而不是让Auto Layout限制它。自动布局取代了设置框架或边界。你不想把两者混合在一起。

答案 3 :(得分:1)

如果您没有使用自动布局,则需要编辑图像框。例如,如果要减小10像素的大小:

{{1}}