可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am currently using a JASidePanel for my application and I have a UITableViewcontroller with a UIRefreshControl as one of the ViewControllers for it. The width of my tableview still has a width of 320 pixels so the UIRefreshControl gets centered right in the middle of the view. I'm trying to figure out if there's a way to offset the UIRefreshControl (moving the x by 20 pixels to the left) so that it looks centered when I have my side panel visible.
Thanks!
回答1:
You need to set the frame of the UIRefreshControl
. Use this code
UIRefreshControl *refContr=[[UIRefreshControl alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
[refContr setTintColor:[UIColor blueColor]];
[refContr setBackgroundColor:[UIColor greenColor]];
[stocktable addSubview:refContr];
[refContr setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin)];
[[refContr.subviews objectAtIndex:0] setFrame:CGRectMake(30, 0, 20, 30)];
NSLog(@"subViews %@",refContr.subviews);
Output:
回答2:
Try editing the bounds
. For example, to move the control down +50px:
refreshControl.bounds = CGRectMake(refreshControl.bounds.origin.x,
-50,
refreshControl.bounds.size.width,
refreshControl.bounds.size.height);
[refreshControl beginRefreshing];
[refreshControl endRefreshing];
回答3:
I needed to do this in order to move the UIRefreshControl downwards. My solution was to subclass UIRefreshControl, and overwrite the layoutSubviews method to set a CAAffineTransform translation on each subview. Unfortunately you can't just set a transform on the UIRefreshControl.
Change xOffset and yOffset as necessary.
@interface MyUIRefreshControl : UIRefreshControl
@end
@implementation MyUIRefreshControl
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
view.transform = CGAffineTransformMakeTranslation(xOffset, yOffset);
}
}
@end
回答4:
UIRefreshControl
's frame is automatically managed so trying to change it's position will likely yield in unexpected results (especially between iOS SDK versions). As you've noted, the control is always centered horizontally in it's superview's frame. Knowing this you can 'take advantage' of the situation by offsetting your superview.
In your case, set your table view's frame to CGRect(-40, 0, 360, superViewHight)
. This will cause your table view to sit 40 points to the left of the window. So you will need to adjust your table view's content accordingly so that it sits 40 points to the right or just extends an extra 40 points, but the stuff rendering off screen should be padding.
Once you do this your UIRefreshControl
will sit 20 points to the left as you desire due to the fact that it is centered.
回答5:
For Swift 2.2 solution is
let offset = -50
let refreshControl = UIRefreshControl()
refreshControl.bounds = CGRect(x: refreshControl.bounds.origin.x, y: offset,
width: refreshControl.bounds.size.width,
height: refreshControl.bounds.size.height)
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(networking), forControlEvents: UIControlEvents.ValueChanged)
self.profileTable.addSubview(refreshControl)
Thats it.
回答6:
- (UIRefreshControl *)refreshControl
{
if (!_refreshControl)
{
_refreshControl = [UIRefreshControl new];
_refreshControl.tintColor = [UIColor lightGrayColor];
_refreshControl.bounds = CGRectInset(_refreshControl.bounds, 0.0, 10.0); // let it down with inset 10.0
[_refreshControl addTarget:self
action:@selector(pullToRefresh)
forControlEvents:UIControlEventValueChanged];
}
return _refreshControl;
}
回答7:
Here's a Swift version similar to the workaround that Werewolf suggested. I'm using my own custom activity view class (MyCustomActivityIndicatorView
) and it's also a subview of the refresh control, so I make sure I don't mess with it's frame, just the frame of the default subviews. After calling layoutSubviews
on super I adjust the custom activity view's frame to match. This is all contained in a custom UIRefreshControl
subclass.
override func layoutSubviews() {
for view in subviews {
if view is MyCustomActivityIndicatorView {
continue
} else {
// UIRefreshControl sizes itself based on the size of it's subviews. Since you can't remove the default refresh indicator, we modify it's frame to match our activity indicator before calling layoutSubviews on super
var subFrame = view.frame
subFrame.size = activityView.intrinsicContentSize()
// add some margins
subFrame.offsetInPlace(dx: -layoutMargins.left, dy: -layoutMargins.top)
subFrame.insetInPlace(dx: -(layoutMargins.left+layoutMargins.right), dy: -(layoutMargins.top+layoutMargins.bottom))
view.frame = subFrame.integral
}
}
super.layoutSubviews()
activityView.frame = bounds
}
Note: I'm adding in UIView's layout margins in which isn't strictly necessary but gives my activity indicator some space to breath.