Autoresize not working in xib

2020-07-14 05:36发布

I made simple design in .xib, deployment target for my app is iOS 5 My view doesn't resizes, when i run my app in simulator or device.

here is snapshot of my .xib file..

When i try to run my app in simulator, it runs properly in the simulator of 4", but it does not resizes, when i run my app in the simulator of 3.5". Autoresize subview property is checked.

Here is snapshot of my Simulaotrs, first screenshot is for 3.5" and second for 4"..

Thanks in advance.

7条回答
我想做一个坏孩纸
2楼-- · 2020-07-14 05:47

could you please use next setting in your .xib file (416 = 480 - 20 (width for status bar) - 44(width for nav bar) enter image description here

416 = 480 - 20 (width for status bar) - 44(width for nav bar)

if you want icon in the top on iPhone 5 use next setting for icons: enter image description here enter image description here

if you want icon in the bottom on iPhone 5 use next setting for icons: enter image description here enter image description here

查看更多
贪生不怕死
3楼-- · 2020-07-14 05:59

if you want to autoresize and autoorder the icons referencing to the upper view, try to play either with the layout tab
layout

or create for each layout own .xib ;)

查看更多
相关推荐>>
4楼-- · 2020-07-14 06:02

I had a similar effect I wanted to achieve with a grid of buttons. I think you'll find the autoresizing that iOS 5 supports simply won't do it. In the end I had two Storyboards, one for iOS 6 devices and another for iOS 5. My App Delegate then checked to see if the main view responded to the constraints method of UIView (which tells you if it supports the new iOS 6 constraints-based layout) and loaded the relevant Storyboard.

Something like this in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method:

if (![self.window.rootViewController.view respondsToSelector:@selector(constraints)]) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_BC_iPhone" bundle:nil];
    UITabBarController *rootController = [storyboard instantiateInitialViewController];
    [self.window setRootViewController:rootController];
}

The iOS 6 Storyboard is loaded by default, but this overrides it with the backward compatible one if need be. The backward compatible Storyboard is manually set out for a 3.5" display only since the iPhone 5 will only run iOS 6 minimum, and thus the backward compatible Storyboard will not need to run on a 4" display.

Setting up the backward compatible Storyboard didn't take long - just duplicate your iOS 6 one that uses constraints, then set it to a 3.5" display, turn constraints off and fix up the views.

An alternative could be to layout the icons programmatically based on the screen dimensions. The advantage of this is you could design it to accommodate any screen size, but the downside is this is probably going to be a lot more effort.

[Edit] Noted you're using a .xib rather than Storyboards, but the theory is the same. Check if UIView responds to constraints then load the appropriate NIB (.xib) file accordingly.

查看更多
聊天终结者
5楼-- · 2020-07-14 06:05

Check whether you have unchecked the Autolayout option in .xib file..enter image description here

查看更多
我想做一个坏孩纸
6楼-- · 2020-07-14 06:06

The solution is so easy.
Add UIVIew on your super view and then add all icons onto them like this (notice the view size starting from the navigation controller to the tab bar)
enter image description here

This subview (the one has your icons) should have these autoresizing masks enter image description here

  • And your icons should have the same autoresizing masks.
查看更多
做个烂人
7楼-- · 2020-07-14 06:06

As I see it you have a couple of options depending on how your layout is set up and how you want to solve it.

If you are using Autolayout, there would be Contraints on your elements, and that could make it impossible to display all your elements in the view, even if you use resize. They just won't fit. So in that scenario, I suggest you use a scrollview in the 480 resolution to make your view being able to be displayed.

Autolayout disabled

In your .h file, make an IBOutlet to your view:

@property (strong, nonatomic) IBOutlet UIView *contentView;

In your .m file, set up like so.

//Add scrollview
UIScrollView *scrollView=(UIScrollView *)self.view;
scrollView.contentSize=self.contentView.frame.size;
[self.view addSubview:self.contentView];

Another work around making it possible for you to place your elements wherever you feel like with different spacings etc. would be to make two different .xib files and set it up like this in you ViewController:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        if([UIScreen mainScreen].bounds.size.height == 568.0)
        {
            //Use iPhone5 VC
            self = [super initWithNibName:@"RootViewController-568h" bundle:nibBundleOrNil];
        }
        else{
            //Use Default VC
            self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        }
    }
    return self;
}

This is if you call your ViewController RootViewController and this way you are safe if they do decide to expand the screen size on the coming iPhone5s or iPhone6. It won't look great but at least it won't crash the app.

查看更多
登录 后发表回答