在Swift中垂直居中的IndicatorImage

前端之家收集整理的这篇文章主要介绍了在Swift中垂直居中的IndicatorImage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码自定义图像放入backIndicator Image中:
var backButton = UIImage(named: "locationbar_back")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationController!.navigationBar.backIndicatorImage = backButton
self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backButton

图像略高于标准图形,所以我想要垂直居中.

我已经尝试在导航栏中找到图像(不是很优雅)使用这个代码(任何更好的建议欢迎):

for myview in self.navigationController!.navigationBar.subviews as [UIView]
{
    if myview.frame.width == 17 //need a better way of identifying object
    {
        var newFrame = myview.frame
        newFrame.origin.y = 6.0
        myview.frame = newFrame
    }
}

令人沮丧的是,图像暂时在正确的地方出现,然后被恢复到原来的位置.

我试过这样说:

> viewDidLoad
> viewWillLayoutSubviews
> viewDidLayoutSubviews
> viewDidAppear

没有快乐

有没有任何建议吗?

您可以通过下一个代码直接更改您的UIImage位置.
原产地可以手动设置:
...
var backButtonImage = UIImage(named: "backButtonImage")!
backButtonImage = changeImagePosition(backButtonImage,origin: CGPointMake(0,0))

navigationController!.navigationBar.backIndicatorImage = backButtonImage
navigationController!.navigationBar.backIndicatorTransitionMaskImage = backButtonImage
...
func changeImagePosition(image: UIImage,origin: CGPoint) -> UIImage {
    let size = image.size

    UIGraphicsBeginImageContextWithOptions(size,false,2)
    image.drawInRect(CGRectMake(origin.x,origin.y,size.width,size.height))

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

猜你在找的Swift相关文章