简体   繁体   English

如果使用自定义子类替换默认视图,则cornerRadius不起作用

[英]cornerRadius not working if replacing default view with custom subclass

Following setup: 以下设置:

i have a popupViewController that has a custom UIView subclass as its view, done in loadView 我有一个popupViewController,它有一个自定义的UIView子类作为其视图,在loadView完成

- (void)loadView
{
    CGRect startingPopupSize = CGRectMake(0.0f, 0.0f, 300.0f, 160.0f);
    _popupView = [[MoviePopupView alloc] initWithFrame:startingPopupSize];
    self.view = _popupView;
}

in this UIView Subclass i have the following code in it's init method 在这个UIView子类中,我在它的init方法中有以下代码

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
        self.layer.masksToBounds = YES;
        self.layer.opaque = NO;
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderWidth = 1.0f;
        self.layer.cornerRadius = 10.0f;
    }
    return self;
}

the problem is, the cornerRadius does not work for this view, but the border gets drawn, see here: 问题是, cornerRadius不适用于此视图,但边框被绘制,请参见此处:

没有cornerRadius

if i don't replace this view with the default uiview of the uiviewcontroller and instead add it as a subview, the cornerRadius works just fine (i want to replace it for several reasons). 如果我不使用uiviewcontroller的默认uiview替换此视图,而是将其添加为子视图,则cornerRadius工作正常(我想替换它有几个原因)。

any ideas why this is not working? 任何想法为什么这不起作用?


Edit: 编辑:

if i just move the layers masksToBounds = YES property from the initWithFrame Method to the drawRect method it works. 如果我只是将来自initWithFrame方法的masksToBounds = YES属性移动到drawRect方法,它就可以工作。 moving it to its viewcontrollers viewDidLoad doesn't. 将它移动到viewcontrollers viewDidLoad没有。

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    self.layer.masksToBounds = YES;

}

any ideas why this works? 任何想法为什么这个工作? i think it is not the right place to set this property here. 我认为这不是在这里设置这个属性的合适地方。

You should make two views: 你应该提出两个观点:

  • background view 背景视图
  • text view 文本视图

For background view set color you want for your text background. 对于背景视图设置您想要的文本背景颜色。 For text view set background color as clear color. 对于文本视图设置背景颜色作为清晰的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIImageView的cornerRadius无法正常工作 - cornerRadius of UIImageView not working UITableViewCell自定义子类视图未更新 - UITableViewCell custom subclass view not updated 自定义uitabbar子类uitabbarcontroller还是开发自定义视图? - custom uitabbar subclass uitabbarcontroller or develop custom view? iPhone:自定义DataGrid视图,子类UITableView,还是其他? - iPhone: Custom DataGrid View, Subclass UITableView, or other? 根据子类中的UIButton大小设置cornerRadius的最佳位置? - Best place to set cornerRadius based on UIButton size in subclass? 在iOS 8上使用UIViewAnimationOptionTransitionFlipFromLeft的动画查看丢角半径 - View lose cornerRadius with Animation of UIViewAnimationOptionTransitionFlipFromLeft on iOS 8 CAShapeLayer cornerRadius在UIBezierPath上不起作用 - CAShapeLayer cornerRadius isn't working on UIBezierPath 在自定义视图/ uiview子类上实现iphone的复制/粘贴控件 - Implementing iphone's copy/paste controls on a custom view / uiview subclass 子类UIViewController或在非全屏视图时创建自定义NSObject - Subclass UIViewController or create a custom NSObject when the view is not fullscreen 自定义UIPageControl视图,将点替换为“ Y的第X页” - Custom UIPageControl view, replacing dots with “Page X of Y”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM