我正在尝试将图像作为ipad应用程序中viewcontroller的背景。我用
设置了这个UIImage *background = [UIImage imageNamed: @"Pilot@2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
这适用于纵向模式,但是当我将其旋转到横向模式时,会出现图片无法填充的空白区域。基于其他类似帖子的我的想法是在轮换发生变化时调用一个新图片,我尝试了这个
{ if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
UIImage *background = [UIImage imageNamed: @"Pilot@2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
UIImage *background = [UIImage imageNamed: @"PilotLandscape@2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}}
这导致了同样的事情。如何解决这个问题的任何想法将不胜感激!
答案 0 :(得分:0)
检查视图的自动调整设置,尝试NSLog视图的坐标,注意uiimageview的UIViewContentModeScale属性。为什么不简单地将imageview添加到故事板?
答案 1 :(得分:0)
工作代码:
@interface SOViewController (){
UIImageView *imageView;
}
@end
-(void)initialOrientationHandling
{
//[self.view removeFromSuperview];
if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeLeft ||
[[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeRight){
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}else if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationPortrait ||
[[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown){
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}
else if([[UIDevice currentDevice]orientation]== UIDeviceOrientationFaceUp){
if(self.view.frame.size.height < 900){
}else{
}
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self initialOrientationHandling];
}
显然,如果没有高度像素化,图像将会失真。我建议你使用不同的旋转。
答案 2 :(得分:0)
(SWIFT):
首先创建两个图像,一个是横向视图大小,另一个是肖像。
初始化imageview
var Imgview = UIImageView()
在viewWillAppear方法中,它在视图加载时以及旋转时更改图像。
override func viewWillAppear(animated: Bool) {
Imgview.frame = self.view.frame
Imgview.contentMode = UIViewContentMode.ScaleAspectFill
Imgview.clipsToBounds = true
view.addSubview(Imgview)
// get notification while rotating
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotate", name: UIDeviceOrientationDidChangeNotification, object: nil
)
}
旋转设定图像的功能,在通知中心调用。
func rotate()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
Imgview.frame = self.view.frame
Imgview.image = UIImage(named: "landscapeSizeimage.png")
self.view.sendSubviewToBack(Imgview)
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
Imgview.frame = self.view.frame
Imgview.image = UIImage(named: "PortraitSizeimage.png")
self.view.sendSubviewToBack(Imgview)
}}
这对我有用。希望它可以帮助某人。我不知道可能会创建更多的图像视图,因此我们可以根据您的意愿删除imageview,而不是带来视图。