Thursday, February 4, 2016

Using ios storyboard constraint for view (wkwebview) object created at runtime

ios xcode storyboard provides easy way to control the view object's layout by constraints. But the constraints only work in interface builder with design time UIView object. If you need to create UIView object at runtime, such as wkwebview, then the constraint will not help.

One option is defining a simple UIView object at design time in storyboard as place holder, this UIView object can be used to define all the required constraint in interface builder. And then at runtime, after the wkwebview or any other view object is created, just add the new view object as the subview in the place holder UIView object, and also set the new view object's autoresizingmask to let it automatically change its size based on the place holder's size and location.

- (void)viewDidLoad {
    [super viewDidLoad];
     WKWebViewConfiguration *theConfiguration =
    [[WKWebViewConfiguration alloc] init];
    theConfiguration.preferences.javaScriptEnabled = true;
    
    CGRect rect = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
    self.theWebView = [[WKWebView alloc] initWithFrame: rect configuration: theConfiguration];
    self.theWebView.navigationDelegate = self;
    
    self.theWebView.autoresizingMask =
    //   UIViewAutoresizingFlexibleLeftMargin|
    //   UIViewAutoresizingFlexibleBottomMargin ;
    //   UIViewAutoresizingFlexibleRightMargin |
    //   UIViewAutoresizingFlexibleTopMargin  |
        UIViewAutoresizingFlexibleHeight    |
        UIViewAutoresizingFlexibleWidth;
    
    
    [self.containerView addSubview:self.theWebView];

}

1 comment:

  1. One of the key features said to be coming to
    the HomePod is the ability to make or receive phone calls
    Read More At iOS Swift Online Training

    ReplyDelete