I have created a view in interface builder that contains another view (content area) and a button bar at the bottom.
The hierarchy is as:
->View
--->mapContainer UIView
----->map MKMapView
----->OverlayView UIView
--->ToolBar UIToolBar
I would like the mapContainer to resize to full window when the ToolBar is hidden.
I would like the map and the OverlayView to resize to the mapContainer size
I have attempted the following code, but it has no effect. Please advise?
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.mapContainer.AutosizesSubviews = true ;
this.View.AutosizesSubviews = true ;
try
{
this.mapContainer.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth ;
this.map.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth ;
this.map.SizeToFit();
this.mapContainer.SizeToFit();
this.map.SizeToFit();
this.View.Frame = new System.Drawing.RectangleF(0,0,this.View.Frame.Width, this.View.Frame.Height );
this.mapContainer.LayoutSubviews();
}
catch(Exception ex)
{
Console.Write(ex.ToString());
}
}
You could also try to redraw the subviews in an overridden method public override void LayoutSubviews with your class.
There you can reset the frames of all your frames. The method SizeToFit() has a tendency to not work as it's name sounds... So first size to fit, then reset the frame.
Hope it helps :)
Check out this article may be this can help you.Its not related to monotouch but it will give nice concept of how to arrange elements together in IB.
http://programmingobsession.blogspot.com/2009/05/real-world-iphone-rotation-part-1.html
Related
I can't understand that the view does not fit on the parent view ? This screenshot shows problem with green button that button doesn't fit parent view. Red background color it is containerView.
I'm using SnapKit for constraints. Please help me. Thanks!
Screenshot of the result
private lazy var scrollView = UIScrollView()
private lazy var containerView = UIView()
// etc
// viewDidLoad
scrollView.addSubview(containerView)
containerView.addSubviews([boxView, addButton])
boxView.addSubviews([titleLabelView, vStackView])
view.addSubview(scrollView)
// viewWillLayoutSubviews
scrollView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
containerView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.width.equalToSuperview()
}
addButton.snp.makeConstraints {
$0.height.equalTo(65)
$0.top.equalTo(boxView.snp.bottom).offset(24)
$0.leading.trailing.equalToSuperview().inset(40)
}
Without the makeConstraints() method can't be certain, but if it's just applying normal autolayout constraints you will need to use a negative value for the bottom constant.
In AL a positive value means move down from the anchor, whereas you need to move the edge of the button up from the anchor (the box's bottomAnchor).
If you've come from Interface BUilder this isn't obvious as in IB the negative values are applied by tool.
$0.top.equalTo(boxView.snp.bottom).offset(-24)
Has anyone come across the NSToolbar width not being correct in relation to the window width when hiding the title visibility? It seems to be the toolbar isn't preserving the correct size after a quit and restart of the app.
I'm using this in my NSWindow Subclass:
self.window!.titleVisibility = NSWindowTitleVisibility.Hidden
When doing so after the restart of my app the far right hand toolbar items aren't hugging the edge of the window and i can see the toolbar isn't being redrawn to the full extent...
I had the same issue. I solved it by removing the toolbar and setting the same toolbar again using GCD (which will actually execute a little later).
Create a subclass of NSWindow and set this class in Interface Builder. Add this to your awakeFromNib:
-(void)awakeFromNib
{
self.titleVisibility = NSWindowTitleHidden;
NSToolbar* toolbar = self.toolbar;
self.toolbar = nil;
dispatch_async(dispatch_get_main_queue(), ^{
self.toolbar = toolbar;
});
}
I found setting the titleVisibility in windowDidLoad() fixed the problem.
override func windowDidLoad() {
super.windowDidLoad()
self.window!.titleVisibility = NSWindowTitleVisibility.Hidden
}
Using the Interface Builder, I created a view with a UIScrollView in it.
I programmaticly add the buttons to the empty UIScrollView.
When the orientation changes, I use
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
to call a method that resets the buttons on screen.
After that, the uiScrollView-content gets a new size using setContentSize.
No matter the width of the ContentSize, I can only interact (scroll/or tap a button) on the first 320px of the screen - which is the screen width in portrait mode.
When I set the contentSize-width to 2000, I can scroll to the left, but only with my fingers on the first 320 px instead of the full 480 (using a 3.5 inch iPhone).
What am I missing?
You need to resize the scrollview's frame, not just the content size (in fact, often you don't need to resize the content size, as the content may not change following an orientation change, but just the frame).
I had the same problem with a Popup (UIView) that was called from a (UIViewController).
In the UIViewController the popup was created as followed
func createPopup() {
let myPopup = MyPopup(frame: UIScreen.mainScreen().bounds)
self.view.addSubview(myPopup)
}
In the Popup (UIView) I needed to override layoutSubviews as follows:
override func layoutSubviews() {
super.layoutSubviews()
self.setNeedsDisplay()
let mainScreenBounds = UIScreen.mainScreen().bounds
view.frame = mainScreenBounds
super.frame = mainScreenBounds <-- Need to update the parent
** perform additional rotation/orientation code here **
}
I'm using a UIScrollView for the first time, and I'm going crazy. I have several text boxes and labels on the scrollview. My text box event handled for TouchedDown, HandleTbLocationTouchDown (tb = Textbox), isn't firing half the time. My guess would be that the scrollview is swallowing the event because I have the exact same functionality on another View and it works perfectly fine.
Also, I set the size of the ScrollView in IB but that doesn't let it be scrollable. Setting it programmatically in my ViewDidLoad works though.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Set the scrollableview
scrollView.Frame = new RectangleF(0,20, 320, 460);
// Set the initial scrollable view area size
scrollView.ContentSize = new SizeF(320, 550);
// For some reason this isn't always firing to do the offset,
// but the keyboard ALWAYS comes up.
tbLocation.TouchDown += HandleTbLocationTouchDown;
}
void HandleTbLocationTouchDown (object sender, EventArgs e)
{
scrollView.SetContentOffset(new PointF(0, 130), true);
}
Help?
I have a UIScrollView that contains a custom UIView. Inside the custom UIView, I'd like to know the rectangle in which it's visible (i.e. not clipped).
The quick-n-dirty solution is to have the custom UIView assume that the parent is a UIScrollView and get the content size through it, but I'm looking for a better solution that doesn't involve make such assumptions.
This should do the trick
CGRect visibleRect = CGRectIntersection(self.frame, superview.bounds);
Use that in the UIView and it should get you the rectangle (if any) that represents the visible section of that view in it's superview (The UIScrollView). I'm assuming here that there is no view between them in the hierarchy, but if there is, fiddling the code should be trivial.
Hope I could help!
It would help if you would give more info on what is that you are trying to accomplish.
If you want to know the size of the super view you can do this:
CGRect superFrame = [self superview].frame;
Swift 3
extension UIView {
var visibleRect: CGRect? {
guard let superview = superview else { return nil }
return frame.intersection(superview.bounds)
}
}