来自xib的自定义UIView未出现在UIScrollView中

时间:2015-10-28 06:54:21

标签: ios objective-c uiview uiscrollview xib

我有一个我在xib文件中创建的自定义UIView。现在在另一个视图控制器中,我在故事板中添加了一个滚动视图。在这个视图控制器的实现文件中,我试图添加这个自定义UIView,但它没有显示。我确定我遗失了一些东西,因为我仍然习惯使用xib文件。我尝试添加普通的UIViews,并在滚动视图中正确显示。这是代码:

[[NSBundle mainBundle] loadNibNamed:@"CustomUIView" owner:nil options:nil];
for (int i = 0; i < 10; i++) {
    CustomUIView *setView = [[CustomUIView alloc] initWithFrame:CGRectMake(10, 10 + (10*i) + (i*100), self.view.frame.size.width - 20, 100)];

    [self.scrollView addSubview:setView];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 10 + (10*10) + (10*100));

框架的定位适用于在此循环中以编程方式创建的UIViews,因此我不认为这是问题所在。有任何想法吗?感谢。

2 个答案:

答案 0 :(得分:0)

使用此代码

namespace My\MainBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use My\MainBundle\Entity\Pro_profile;
use My\MainBundle\Entity\Pro_education;
use My\MainBundle\Form\Pro_profileEducationType;
use Symfony\Component\HttpFoundation\Session\Session;
use Application\Sonata\UserBundle\Entity\User;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class RegistrationSpecialEducationController extends Controller
{ 
    /**
     * Creates a new Pro_profile entity.
     *
     * @Route("/create_education", name="pro_profile_create_education")
     * @Method("POST")
     * @Template("MyMainBundle:RegistrationSpecialEducation:education.html.twig")
     */ 
    public function createAction(Request $request)
    {
       $user = $this->get('security.token_storage')->getToken()->getUser()->getId();

        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('MyMainBundle:Pro_profile')->findOneBy(array('user' => $user));


       // $educations = $entity-> getEducations();

        //$scan = $educations -> getScan();

        //foreach ($educations as $edu) {
//
        //    $tabela = $edu ->getScan();
       // }



        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);



            if ($form->isValid()) {
                $em = $this->getDoctrine()->getEntityManager();

                $entity->getEducations() ->upload();

                $em->persist($entity);
                $em->flush();

                $this->redirect($this->generateUrl('RegisterSpecialEducation'));
            }



        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
           // 'edu' => $tabela,
        );
    }

    /**
     * Creates a form to create a Pro_profile entity.
     *
     * @param Pro_profile $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(Pro_profile $entity)
    {
        $form = $this->createForm(new Pro_profileEducationType(), $entity, array(
            'action' => $this->generateUrl('pro_profile_create_education'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Zakończ rejestracje'));

        return $form;
    }

    /**
     * @Route("/register/special/education", name="RegisterSpecialEducation")
     * @Template()
     */
    public function educationAction()
    {
        $user = $this->get('security.token_storage')->getToken()->getUser()->getId();

        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('MyMainBundle:Pro_profile')->findOneBy(array('user' => $user));

        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }


}

答案 1 :(得分:0)

在您的具体情况下,更好的方法是使UIView成为UITableViewCell并使用它填充UITableView而不是运行循环并在{{1中添加子视图}}。我很高兴它解决了你的问题并同时使你的应用程序更好。

但接近一点,如果你想知道为什么你的代码不工作,问题是你如何加载Nib文件。 This已经回答的问题将清除所有关于加载笔尖的概念,并向您展示以最稳定的方式加载它的正确方法。快乐的编码!