【问题标题】:How to make the UIImage fit the whole UINavigationBar view's size如何使 UIImage 适合整个 UINavigationBar 视图的大小
【发布时间】:2014-12-09 12:25:56
【问题描述】:

我需要为整个应用的UINavigationController的bars设置背景图片,所以我写了如下代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

    let backgroundImage = UIImage(named: "NavBar")
    UINavigationBar.appearance().setBackgroundImage(backgroundImage, forBarMetrics: .Default)

    return true
}

但是我需要使UIImage 适合酒吧的大小,因为在我的情况下它太大并且不适合整个酒吧。我该怎么做?

提前致谢。

【问题讨论】:

    标签: ios xcode interface-builder xcode6 uinavigationbar


    【解决方案1】:

    它应该会自动重复以填充导航栏的大小,但如果您希望它伸展,您可以更改设置资产目录中的切片插图:

    https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/chapters/SlicinganImage.html

    或者在代码中是这样的:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        let backgroundImage = UIImage(named: "NavBar")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 15, 0, 15), resizingMode: UIImageResizingMode.Stretch)
    
        UINavigationBar.appearance().setBackgroundImage(backgroundImage, forBarMetrics: .Default)
    
        return true
    }
    

    【讨论】:

      【解决方案2】:

      您可以轻松更改图像视图的大小:

      // Set the size hard coded
      CGRect myFrame = CGRectMake(0,0,320,200); 
      // Or you can get the size from the view
      CGRect myFrame = backGroundView.frame
      
      // Now assign the size to the image, I normally do this is an animation
      [UIView beginAnimations:NULL context:NULL];
      [UIView setAnimationDuration:0.5];
      imageView.frame = myFrame;
      [UIView commitAnimations];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 2021-06-22
        相关资源
        最近更新 更多