【问题标题】:Centering a UIView居中 UIView
【发布时间】:2011-07-04 08:32:15
【问题描述】:

我是 Cocca 和 IOS 开发的新手,发现自己处于以下情况。

我有一个 1024x1024 的背景图像,我想在所有视图上显示它。我已将以下代码放入应用程序委托中的应用程序 didFinishLaunchingWithOptions 中,它可以正确加载并且可以在所有视图下查看。

我的问题是我似乎无法将背景 UIView 居中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Setup background image for all views
UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background-image.png"]];
[window addSubview : backgroundView];
[backgroundView release];


// Override point for customization after application launch.
[window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

return YES;
}

我已经设法将背景 UIView 移动到不同的位置,但不是中心。

我会为此提供任何帮助。

提前致谢 马特

【问题讨论】:

    标签: xcode ipad ios uiview


    【解决方案1】:

    尝试用...替换您的代码

    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image.png"]];
    backgroundView.frame = window.frame;
    [window addSubview:backgroundView];
    [backgroundView release];
    

    ...如果您的 background-image.png 不适合整个窗口,请尝试使用 backgroundViewautoresizingMaskcontentMode

    【讨论】:

    • 非常感谢@Chiefly Izzy 的回复,您的代码和添加的 backgroundView.contentMode = UIViewAutoresizingFlexibleWidth;做了我需要的。
    • 这不是它应该如何工作的:backgroundView.contentMode = UIViewAutoresizingFlexibleWidth。相反,您应该使用UIViewContentModeScaleAspectFill。你搞砸了枚举类型。
    【解决方案2】:

    使用 setFrame 方法使图像居中,使用 autoresizingMask 属性保持居中

    enum {
       UIViewAutoresizingNone                 = 0,
       UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
       UIViewAutoresizingFlexibleWidth        = 1 << 1,
       UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
       UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
       UIViewAutoresizingFlexibleHeight       = 1 << 4,
       UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多