如何使用自定义背景图像从 UINavigationBar 和 UIToolbar 中删除圆角?
Posted
技术标签:
【中文标题】如何使用自定义背景图像从 UINavigationBar 和 UIToolbar 中删除圆角?【英文标题】:How to remove rounded corners from UINavigationBar and UIToolbar with custom background images? 【发布时间】:2011-08-15 16:40:56 【问题描述】:在基于拆分视图的 iPad 应用程序中,我在导航栏和工具栏中使用自定义背景图像。
我对UINavigationBar
和/或UIToolbar
进行了子类化,在drawRect:
我有(背景图像是矩形):
- (void)drawRect:(CGRect)rect
CGContextRef c = UIGraphicsGetCurrentContext();
UIImage *image = [UIImage imageNamed:@"background-image"];
CGContextDrawImage(c, rect, image.CGImage);
结果是 我想从中删除圆角。
我已经尝试过的:
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
结果和上面一样,
clipsToBounds = YES;
在我的子类中,
cornerRadius=0.0
,
iPhoneOS SDK - Remove Corner Rounding from views (iPad problem)
4. 中描述的问题与我遇到的完全相同。不幸的是,该问题中描述的解决方案对我不起作用。当从viewWillAppear
在纵向方向调用时,它出于某种原因隐藏了我的导航项。在 Landscape 中从 didRotateFromInterfaceOrientation:
调用时,圆角消失了,但同时所有用户交互都被禁用。此外,当旋转回纵向时,我的“主”视图(拆分视图的左侧)不会隐藏。
我使用接受答案中的代码得到的输出是:
aView: <UIWindow: 0x5e18470; frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x5e18280>>
subview: <UILayoutContainerView: 0x6942000; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x6946b30>>
aView: <UILayoutContainerView: 0x6942000; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x6946b30>>
subview: <BackgroundImageView: 0xbbacf70; frame = (0 0; 768 1004); layer = <CALayer: 0xbbad020>>
aView: <BackgroundImageView: 0xbbacf70; frame = (0 0; 768 1004); layer = <CALayer: 0xbbad020>>
subview: <UILayoutContainerView: 0x69144e0; frame = (321 0; 703 748); clipsToBounds = YES; layer = <CALayer: 0x6946fe0>>
aView: <UILayoutContainerView: 0x69144e0; frame = (321 0; 703 748); clipsToBounds = YES; layer = <CALayer: 0x6946fe0>>
subview: <UILayoutContainerView: 0xbbabc40; frame = (0 0; 320 748); clipsToBounds = YES; layer = <CALayer: 0xbbabc90>>
aView: <UILayoutContainerView: 0xbbabc40; frame = (0 0; 320 748); clipsToBounds = YES; layer = <CALayer: 0xbbabc90>>
subview: <UIImageView: 0x5e178c0; frame = (321 0; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba60d10>> - (null)
aView: <UIImageView: 0x5e178c0; frame = (321 0; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba60d10>> - (null)
subview: <UIImageView: 0xba74010; frame = (317 0; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba62180>> - (null)
aView: <UIImageView: 0xba74010; frame = (317 0; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba62180>> - (null)
subview: <UIImageView: 0xbad0110; frame = (321 745; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba60da0>> - (null)
aView: <UIImageView: 0xbad0110; frame = (321 745; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba60da0>> - (null)
subview: <UIImageView: 0xba60e00; frame = (317 745; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba60e30>> - (null)
aView: <UIImageView: 0xba60e00; frame = (317 745; 3 3); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xba60e30>> - (null)
还有一些小的 3x3px 圆角 UIImageViews。
问题是:如何去除那些微小的圆角?
【问题讨论】:
这个运气好吗?目前我自己也遇到了同样的问题。 【参考方案1】:对我有用的解决方案:
- (void)hideSplitViewCorners
// Hide corners of splitview.
NSMutableArray *views = [NSMutableArray array];
[views addObject:[self view]];
for (NSUInteger i = 0; i < views.count; ++i)
UIView *view = [views objectAtIndex:i];
[views addObjectsFromArray:view.subviews];
if (view.frame.size.width == 3.0 && view.frame.size.height == 3.0)
view.hidden = YES;
我在 UISplitViewController
的子类的 loadView
和 didRotateFromInterfaceOrientation:
中调用它。
请注意,在后者中我需要使用[self performSelector:@selector(hideSplitViewCorners) withObject:NULL afterDelay:0];
。
基本上,需要确保这些角的隐藏是在它们已经显示在屏幕上之后完成的。
【讨论】:
以上是关于如何使用自定义背景图像从 UINavigationBar 和 UIToolbar 中删除圆角?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用自定义背景图像从 UINavigationBar 和 UIToolbar 中删除圆角?