Implementing programatically UIView over a UIViewController

I'm trying to understand which methods of UIView I must override to be able to display something.

I have a very simple application that:

  • Has a single UIViewController

  • Has a UIView

  • The UIView creates an offscreen image with the size of the View and the image is drawn in drawRect

  • Questions:

  • How should I create the view? Calling initWithRect? Or just init? I want to do it programatically, no storyboard nor nibs.

  • What method of UIView is called when the UIViewController sets the view bounds? And how can I get these bounds (to be able to create the offscreen image).

  • Does someone know where can I find the source for UIImageView?

  • Note: I've been trying to do this for 4 days, without success.


    1.) initWithFrame:

    1.) By default, there is no method that's automatically called when bounds are set. If you subclass the UIView you can override the setBounds method if you wish.

    2.) Sources for UIImageView ?..The header class is available in the CoreFoundation.framework .


    That's how im using UIView in UIViewController and added UIImageView and UITextView as subview of UIView in UIViewController didn't showed code for UITextview because you are not asking about it in your question.

    - (void)viewDidLoad
    {
        UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        [self.view addSubview:baseView];
        [baseView release];
    
        // Displays UIImageView
        UIImageView* ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 300, 235)];
        self.view.backgroundColor = [UIColor brownColor];
    
        // load all the frames of our animation
        ImageView.animationImages = [NSArray arrayWithObjects:    
                                        [UIImage imageNamed:@"1a.png"],
                                        [UIImage imageNamed:@"1b.png"],
                                        nil];
    
        // all frames will execute in 25 seconds
        ImageView.animationDuration = 25; 
    
        // start animating
        [ImageView startAnimating];
        ImageView.layer.borderWidth = 2;
        ImageView.layer.borderColor = [[UIColor whiteColor]CGColor];
        [ImageView.layer setMasksToBounds:YES];
        [ImageView.layer setCornerRadius:15.0f];
    
        [baseView addSubview:ImageView]; 
    }
    

    Hope it helps you.


    You need to override -(void)loadView in your custom ViewController class:

    UIView *baseView = [[UIView alloc] init];
    self.view = baseView;
    [super loadView];
    

    From there you can create a custom UIView that adds the view property of that ViewController.

    链接地址: http://www.djcxy.com/p/87736.html

    上一篇: 在Swift中用UIViewController的自定义初始化,并在故事板中设置界面

    下一篇: 通过UIViewController实现编程式的UIView