my problem is the following:
I have an UIViewController initialized in a NIB. In this nib, I have created another UIView (call it view XY) and linked it to an IBOutlet inside the UIViewController.
I have then added some UIImageView area exclusively via code and added them to view XY.
This is a portion of code:
-(void) displayImage{
XYImageView=[[UIImageView alloc]init];
[imageContainer insertSubview:XYImageView atIndex:0];
[XYImageView setFrame:imageContainer.frame];
//some code that loads an UIImage into the XY view
XYImageView.contentMode = UIViewContentModeScaleAspectFit;
}
Problem is that my image appears resized correctly, BUT it is collocated outside the XYImageView area. I cannot understand why.
Also, if I try to log the view's size and origin, I get all 0's.
Maybe I am missing something?
Thank you guys.
Frame property defines view position in coordinate system of its parent, so your xyimageview gets the same coordinates as its parent has in its superview. To set child view completely fill its superview try to do the following (i.e. use bounds property, not frame):
[XYImageView setFrame:imageContainer.bounds];
Related
there
I have a viewController ,the hierarchy is something like this
UIView->imageview->uiview->uiimageView->uitableview.
Now i want to change height of only first two object of my hierarchy i.e UIView and UIImageView without effecting frames of other subviews.Please help me.
According to your original question, this will work for you.
self.view.autoResizeSubviews = NO;
self.myImageView.autoResizeSubviews = NO;
and then resize.
I'm trying to switch from two-stage rotation to one-stage rotation (to avoid the console warning, and because Apple recommend doing so because one-stage is faster).
However I can't figure out how to get the new size of my view (taking into account the navigation bar, status bar, etc) early enough to perform the update of my UI during the animation (rather than simply snapping the items to their new positions at the end as many applications seem to do, which results in a big "jerk" right at the end of the animation).
The sizes I get in the willAnimateRotationToInterfaceOrientation:duration: method are (perhaps obviously) the old sizes.
I can see I should be able calculate it by hand, working out the current bar heights, then inferring the new view frame size by deducted those from the rotated screen dimensions? (which isn't that difficult to do, though is perhaps fragile as it assumes the navigation bar, status bar, etc will be the same height in both orientations, and you'd have to manually take account of the toolbar being different heights in portrait vs landscape - I just want to make sure I've not missed a more straightforward or common way.)
Any feedback on approaches other people have taken would be great!
Thanks
Joseph
I've had the most success using my view's layoutSubviews method for autorotations. When layoutSubviews gets called during an orientation change, the view's bounds are already set to what they will be at the conclusion of the rotation. You can also at this time query the status bar, navigation bar, and toolbar for their sizes and get the correct post-rotation values (although the status bar width and height may be swapped--I just take the smaller value as the height, works great). Using that information you can lay out subviews and they will then be animated as part of the rotation.
It can be annoying to create a UIView subclass for every situation where you want to do this, so I created a special subclass of UIView called DelegatingView, which looks like this:
#protocol DelegatingViewDelegate
- (void)layoutSubviewsForView:(UIView*)view;
#end
#interface DelegatingView : UIView {
id<DelegatingViewDelegate> _delegate;
}
#property (nonatomic,assign) id<DelegatingViewDelegate> delegate;
#end
#implementation DelegatingView
#synthesize delegate = _delegate;
- (void)layoutSubviews {
[super layoutSubviews];
[_delegate layoutSubviewsForView:self];
}
#end
I use this as my base view, add subviews to it, and set my UIViewController as the delegate. Then I can layout subviews from my view controller, having access to the current orientation and so on.
Which OS version are you targeting? Under OS 4.0 (which is what I did a quick test in), [view bounds] within willAnimateRotationToInterfaceOrientation:duration: returns the new, post-rotation bounds.
If you are not seeing that, I’d suggest double-checking that your view has the appropriate auto resize mask set.
If you have complex layout requirements in the view of your view controller, it is worth it to create a subclass of UIView and perform your layout code in -layoutSubviews (the correct place to do view layout). As Hilton Campbell pointed out, if you use this method for layout, you can check frame sizes and subview relationships and set their new positions appropriately.
If your view controller's view has a simple set of subviews, then you should probably just set their autoresizingMask properties appropriately, and let them animate themselves automagically.
I tried two methods to adjust view's size.
1.) notify child views to adjust their size after -(void)viewWillAppear;
The drawback is that it will be executed each time when viewWillAppear.
ex:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self notifyChildViewWillAppear:animated];
}
at child view
- (void)notifyChildViewWillAppear:(BOOL)animated
{
// at this time, the size of superview's bounds
//equals the content view's size
// (no navigationBar's height and toolbar's height.
self.frame = [self superview].bounds;
}
2.) Calculate the necessary size by hand.
I calculate as viewDidLoad.
The sizes of navigationBar and toolBar can be derived by following code.
self.navagationController.navigationBar.view.frame.size.height;
self.tabBarController.tabBar.frame.size.height;
I have a UIScrollView that contains a UIView onto which I am drawing a rather complex graphic using CGPaths. After the view finishes loading the graphic is distorted in that it is elongated horizontally and vertically. If I redraw it, it looks normal.
Any ideas on what is causing this and how to fix it?
When you initialise your UIView, use initWithFrame and set the frame to the correct size that it will be after everything is drawn, etc. If you are not sure here are some ideas:
Get the 'bounds' of the parent view.
Call layoutSubviews on the root view, then get its frame, then create your UIView and add it to its parent.
The the problem arises when i dynamically load subview from nib and add it to the current main view during viewDidLoad.
UIView *someView = // load from nib
someView.frame
= CGRectMake(.0,
.0,
kFormSheetWidth /* = 540.0 */,
kFormSheetHeight) /* = 576.0 */;
[self.view addSubview:someView];
It all happens inside FormSheet window, where UINavigationController instance was passed into presentModalViewController:.
But, someView always resizes itself to be by 220.0 larger and by 116.0 higher and thus doesn't fit into window.
someView nib looks like
UITableView
xx UIView
xxxx UITextInput
xx UIButton
All these views have autoresizeSubviews == YES, clipSubviews == YES.
It's very surprising behavior. Interestingly, when I set the frame of someView in viewWillAppear:, everything looks fine. What (where) should I look for to tackle this thing?
Are you doing this is landscape mode? (ipad I assume because of the dimensions?)
This is a bit of a stab in the dark, but its a problem for a lot of people. (if its not it I appologize)
UINavigationController's rootview doesn't handle the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method like your code does, so you need to subclass UINavigationController and implement it like your view controllers so it doesn't always add the views in portrait.
Some view is resizing itself after you create its frame on the viewDidLoad.
What you should do, is set the autoresizeSubviews to NO or just set it's frame in the viewWillApear.
I have a UITableViewController inside of a UINavigationController.
I want to have a UIView appear over the top of the table view but not susceptible to the scrolling of the table view.
I.e. if the table view is scrolled, the UIView should remain in the same position relative to the screen, rather than relative to the table view. It should appear anchored in a certain position.
What is the best way to achieve this?
EDIT: To clarify, the view should float transparently over the top of the table view.
Many thanks!
I also wanted to have a floating UIView over my tableView.
So, within my RootViewController (which is a UITableViewController), this worked for me
- (void)viewDidLoad {
/* mylabel is a UILabel set in this class */
[self.mylabel setUserInteractionEnabled:NO];
/* navigationController comes from higher up in the navigation chain */
[self.navigationController.view addSubview:self.mylabel];
}
Similar to what Peter said, create a UIView that will contain both the TableView and the subclassed UIView. Such as:
UIView *view = [[UIView alloc] initWithFrame:frame]; // Define frame as you like
[view addSubview:myTableView]; // This is the reference to your tableView
[view addSubview:myAnchoredView]; // This is the reference to your UIView "floating" subclass
You will also need to turn off user interaction for your floating view. I don't know if this will specifically pass the touches to the underlying UIView's or not though:
[myAnchoredView setUserInteractionEnabled:NO];
If this is blocking touches to your tableView, you may need to pass the reference to your tableView to the anchored view at initialization, then pass the touch events along. You can do this by overriding the touch response methods in UIResponder. (If there is a better way, someone please speak up.)
Do you mean the anchored view should appear transparent over the UITableView, or just above, i.e. anchored view uses top 20% of the available space, table view uses the rest?
In any case, create a UIView containing the anchored view and the table view. If you want the anchored view transparent over the table view, it's a bit tricky, because to scroll the table view, touches have to pass through the anchored view.
Add the surrounding view's view controller to the navigation controller instead of just the tableview.
I investigated how UIScrollView keeps its scrollIndicator above the UIScrollView's content and yet unmoving by examining a UIScrollView in the debugger.
The scrollIndicators are UIImageViews. And you can see they are direct descendants of the UIScrollView itself. You can also see that any scrolled content is also a direct descendent. So how is it that the scroll indicators don't move?
I tried updating the position of my static content constantly in - (void)scrollViewDidScroll:(UIScrollView *)scrollView this, surprisingly, works. I'm not sure if it is how UIScrollView itself does it, but without some private magic, it must be something like this.