Недавно я перетронул с помощью раскадровки и xibs в своем приложении. При этом было довольно просто связать изображения с правильным именем изображения. Теперь я хочу связать их без этих проблем и столкнуться с проблемами. Искал довольно много времени здесь и Google без особого успеха.Ссылка на изображение без интерфейса builder
Эти изображения будут использоваться для заставки после запуска приложения, чтобы предотвратить белую вспышку (отлично работает с IB). В моем файле .h я сделал это.
@property (nonatomic, retain) UIImageView *splash4InchPortrait;
Как бы я заявляю, что имя изображения из образа у меня в приложении сказать, [email protected]?
Это то, что у меня есть в обоих файлах. Просто чтобы дать вам представление о том, с чем я работаю.
.h
@interface CenterViewController : UIViewController <PullToRefreshViewDelegate, UIWebViewDelegate, UIGestureRecognizerDelegate, MTStatusBarOverlayDelegate, UIScrollViewDelegate>
{
UIWebView *webView;
NSTimer *timer;
UIScrollView *currentScrollView;
MTStatusBarOverlay *overlay;
}
@property(nonatomic) UIViewAutoresizing autoresizingMask;
@property(nonatomic) UIViewAutoresizing bounds;
@property (nonatomic, retain) UIImageView *splash35InchPortrait;
@property (nonatomic, retain) UIImageView *splash35InchLandscape;
@property (nonatomic, retain) UIImageView *splashRetina35InchPortrait;
@property (nonatomic, retain) UIImageView *splashRetina35InchLandscape;
@property (nonatomic, retain) UIImageView *splash4InchPortrait;
@property (nonatomic, retain) UIImageView *splash4InchLandscape;
@property (nonatomic, retain) UIImageView *splashPadPortrait;
@property (nonatomic, retain) UIImageView *splashPadLandscape;
@end
.m
@interface UIViewController()
@end
BOOL isPortraitView = YES;
@implementation CenterViewController
@synthesize splash35InchPortrait, splash35InchLandscape, splashRetina35InchPortrait, splashRetina35InchLandscape, splash4InchPortrait, splash4InchLandscape, splashPadPortrait, splashPadLandscape;
- (void)viewDidLoad
{
[super viewDidLoad];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.f, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
[(UIWebView*)webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.mysite.com"]]];
webView.contentMode = UIViewContentModeScaleToFill;
webView.scalesPageToFit = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[self.view addSubview:webView];
for (UIView* subView in webView.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
currentScrollView = (UIScrollView *)subView;
currentScrollView.delegate = (id) self;
}
}
webView.scrollView.scrollsToTop = YES;
[webView setDelegate:self];
webView.tag = 999;
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
[self.navigationController setNavigationBarHidden:YES];
[super viewDidLoad];
webView.contentMode = UIViewContentModeScaleAspectFit;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//get screen size
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
//DO Portrait
if (screenBounds.size.height <=480) {
//code for 3.5-inch screen
splash35InchPortrait.hidden = NO;
splash35InchLandscape.hidden = YES;
splashRetina35InchPortrait.hidden = YES;
splashRetina35InchLandscape.hidden = YES;
splash4InchPortrait.hidden = YES;
splash4InchLandscape.hidden = YES;
splashPadLandscape.hidden = YES;
}else{
// code for 3.5 Retina inch screen
splash35InchLandscape.hidden = YES;
splashRetina35InchPortrait.hidden = NO;
splashRetina35InchLandscape.hidden = YES;
splash4InchLandscape.hidden = YES;
splashPadLandscape.hidden = YES;
}
}else{
// code for 4-inch screen
splash35InchPortrait.hidden = YES;
splashRetina35InchPortrait.hidden = YES;
splash4InchPortrait.hidden = NO;
splash4InchLandscape.hidden = YES;
splashPadLandscape.hidden = YES;
}
// Set up Pull to Refresh code
PullToRefreshView *pull = [[PullToRefreshView alloc] initWithScrollView:currentScrollView];
[pull setDelegate:self];
pull.tag = 998;
[currentScrollView addSubview:pull];
//Statusbar Overlay
overlay = [MTStatusBarOverlay sharedInstance];
overlay.delegate = self;
overlay.progress = 0.0;
webView.backgroundColor = [UIColor colorWithWhite:0.1f alpha:1.0f];
// Forward/Back Gestures
UISwipeGestureRecognizer *goForward = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)];
goForward.direction = UISwipeGestureRecognizerDirectionRight;
goForward.delegate = self;
goForward.numberOfTouchesRequired = 1;
[webView addGestureRecognizer:goForward];
UISwipeGestureRecognizer *goBack = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)];
goBack.direction = UISwipeGestureRecognizerDirectionLeft;
goBack.delegate = self;
goBack.numberOfTouchesRequired = 1;
[webView addGestureRecognizer:goBack];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return NO;
}
//Other Gestures
- (void)swipeLeftAction:(id)ignored{
[webView goBack];
[overlay postMessage:@"Go Back" duration:1 animated:YES];
}
- (void)swipeRightAction:(id)ignored{
[webView goForward];
[overlay postMessage:@"Go Forward" duration:1 animated:YES];
}
-(void)pullToRefreshViewShouldRefresh:(PullToRefreshView *)view {
[(UIWebView *)[self.view viewWithTag:999] reload];
[overlay postMessage:@"Reloading" duration:1 animated:YES];
}
//Webview Start/Finish Request
- (void)webViewDidStartLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webview {
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if (isiPhone5) {
// Loading iPhone 5
[(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL],
([email protected]"NO":@"YES"));
}
else if (IS_IPAD) {
// Loading IPAD
[(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL],
([email protected]"NO":@"YES"));
}
else {
// Loading iPhone 3.5"
[(PullToRefreshView *)[self.view viewWithTag:998] finishedLoading];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"didFinish: %@; stillLoading:%@", [[webView request]URL],
([email protected]"NO":@"YES"));
}
}
//Rotation
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){
[webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"];
}
else{
[webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//Image animation
- (void)tick {
if (!webView.loading) {
[self performSelector:@selector(fadeimage) withObject:nil afterDelay:0.3];
}
}
-(void)fadeimage{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
splash35InchPortrait.alpha = 0;
splash35InchLandscape.alpha = 0;
splashRetina35InchPortrait.alpha = 0;
splashRetina35InchLandscape.alpha = 0;
splash4InchPortrait.alpha = 0;
splash4InchLandscape.alpha = 0;
splashPadLandscape.alpha = 0;
splashPadPortrait.alpha = 0;
[UIView commitAnimations];
}
- (void)viewDidUnload {
webView = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
@end
UPDATE:
С кодом слиты из моего старого проекта и кода из принятого ответа ниже (спасибо kushyar). Мне это удалось. Если кому-то интересно, это то, что я сделал.
//Setting Splash Images
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
bool isiPhone5 = CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,CGSizeMake(640, 1136));
if (isiPhone5) {
// Load iPhone 5 Splash
UIImage *splash4Inch = [UIImage imageNamed:@"[email protected]"];
self.splash4InchPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, -20.0f, 320.0f, 568.0f)];
[self.splash4InchPortrait setImage:splash4Inch];
[self.view addSubview:self.splash4InchPortrait];
[self.view bringSubviewToFront:self.splash4InchPortrait];
self.splash4InchPortrait.contentMode = UIViewContentModeScaleToFill;
}
else if (IS_IPAD) {
// Load IPAD StoryBoard
UIImage *splashPad = [UIImage imageNamed:@"Default-Portrait~ipad.png"];
self.splashPadPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, -20.0f, 768.0f, 1024.0f)];
[self.splashPadPortrait setImage:splashPad];
[self.view addSubview:self.splashPadPortrait];
[self.view bringSubviewToFront:self.splashPadPortrait];
self.splashPadPortrait.contentMode = UIViewContentModeScaleToFill;
}
else {
// Load the iPhone 3.5" Splash
UIImage *splash35Inch = [UIImage imageNamed:@"Default.png"];
self.splash35InchPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, -20.0f, 320.0f, 480.0f)];
[self.splash35InchPortrait setImage:splash35Inch];
[self.view addSubview:self.splash35InchPortrait];
[self.view bringSubviewToFront:self.splash35InchPortrait];
self.splash35InchPortrait.contentMode = UIViewContentModeScaleToFill;
}
Голые со мной, поскольку я новичок в использовании только кода. Что конкретно мне нужно изменить, чтобы это сделать? Приведенный выше код. – ChrisOSX
Итак, в вашем файле реализации (.m) у вас есть метод viewDidLoad. Вы можете либо сделать это внутри этого, либо, как я предложил, использовать метод load ({void) loadView {}. Я буду модифицировать свое сообщение для ясности. дай мне минуту. – kushyar
Не годи, пока не появляется. Я только пытаюсь показать изображение Default-568h, пока оно показывается. То, что я использую для моего экрана заставки. Как вы видите по коду, у меня много других заставки. – ChrisOSX