ios 偏移 UIRefreshControl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17309268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Offsetting UIRefreshControl
提问by user754905
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.
我目前正在为我的应用程序使用 JASidePanel,并且我有一个 UITableViewcontroller 和一个 UIRefreshControl 作为它的 ViewControllers 之一。我的 tableview 的宽度仍然有 320 像素的宽度,因此 UIRefreshControl 在视图的中间居中。我试图弄清楚是否有办法偏移 UIRefreshControl(将 x 向左移动 20 像素),以便在我的侧面板可见时它看起来居中。
Thanks!
谢谢!
回答by user1195202
Try editing the bounds
. For example, to move the control down +50px:
尝试编辑bounds
. 例如,要将控件向下移动 +50px:
refreshControl.bounds = CGRectMake(refreshControl.bounds.origin.x,
-50,
refreshControl.bounds.size.width,
refreshControl.bounds.size.height);
[refreshControl beginRefreshing];
[refreshControl endRefreshing];
回答by Warewolf
You need to set the frame of the UIRefreshControl
. Use this code
您需要设置UIRefreshControl
. 使用此代码
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:
输出:
回答by stu
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.
我需要这样做才能将 UIRefreshControl 向下移动。我的解决方案是继承 UIRefreshControl,并覆盖 layoutSubviews 方法以在每个子视图上设置 CAAffineTransform 转换。不幸的是,您不能只在 UIRefreshControl 上设置转换。
Change xOffset and yOffset as necessary.
根据需要更改 xOffset 和 yOffset。
@interface MyUIRefreshControl : UIRefreshControl
@end
@implementation MyUIRefreshControl
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
view.transform = CGAffineTransformMakeTranslation(xOffset, yOffset);
}
}
@end
回答by Chris Wagner
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.
UIRefreshControl
的框架是自动管理的,因此尝试改变它的位置可能会产生意想不到的结果(尤其是在 iOS SDK 版本之间)。正如您所指出的,控件始终在其父视图的框架中水平居中。知道这一点,您可以通过抵消您的超级视图来“利用”这种情况。
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.
在您的情况下,将表视图的框架设置为CGRect(-40, 0, 360, superViewHight)
. 这将导致您的表格视图位于窗口左侧 40 点处。因此,您需要相应地调整 table view 的内容,使其位于右侧 40 点或仅扩展额外的 40 点,但屏幕外渲染的内容应该是填充。
Once you do this your UIRefreshControl
will sit 20 points to the left as you desire due to the fact that it is centered.
完成此操作后,UIRefreshControl
由于它居中,您将按照您的意愿向左放置 20 点。
回答by Rufat Mirza
Swift 5:
斯威夫特 5:
There are a couple of ways to change UIRefreshControl position. The setup of the refresh control is done with the most convenient way.
有几种方法可以改变 UIRefreshControl 的位置。刷新控件的设置以最方便的方式完成。
Example where the refresh control is part of the scroll view:
刷新控件是滚动视图的一部分的示例:
let refresher = UIRefreshControl()
refresher.addTarget... // Add a proper target.
scrollView.refreshControl = refresher
Please note that you can use tableView instead of the scrollView.
请注意,您可以使用 tableView 而不是 scrollView。
Option 1:You can play with the frame/bounds of the view.
选项 1:您可以使用视图的框架/边界。
scrollView.refreshControl?.refreshControl.bounds.origin.x = 50
Option 2:Or you can do it using Auto Layout.
选项 2:或者您可以使用自动布局来完成。
scrollView.refreshControl?.translatesAutoresizingMaskIntoConstraints = false
scrollView.refreshControl?.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
scrollView.refreshControl?.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -50).isActive = true
回答by muhasturk
For Swift 2.2 solution is
对于 Swift 2.2 解决方案是
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.
就是这样。
回答by George
- (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.bounds = CGRectInset(_refreshControl.bounds, 0.0, 10.0); // 用 inset 10.0 放下它
[_refreshControl addTarget:self
action:@selector(pullToRefresh)
forControlEvents:UIControlEventValueChanged];
}
return _refreshControl;
}
回答by Aleksey Mazurenko
You need to subclass UIScrollView, following code will adjust UIRefreshControl location based on contentInset (works only for vertical scrolling but easily adoptable for horizontal)
您需要继承 UIScrollView,以下代码将根据 contentInset 调整 UIRefreshControl 位置(仅适用于垂直滚动,但易于用于水平滚动)
override public func layoutSubviews() {
super.layoutSubviews()
guard let refreshControl = self.refreshControl else { return }
var newFrame = refreshControl.frame
if contentInset.top > .zero {
let yOffset = abs(contentInset.top + contentOffset.y)
newFrame.origin.y = -contentInset.top - yOffset
newFrame.size.height = yOffset
}
guard newFrame != refreshControl.frame else { return }
refreshControl.frame = newFrame
}
回答by Ben Lachman
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.
这是一个类似于 Werewolf 建议的解决方法的 Swift 版本。我正在使用我自己的自定义活动视图类 ( MyCustomActivityIndicatorView
),它也是刷新控件的子视图,所以我确保我不会弄乱它的框架,只是默认子视图的框架。调用layoutSubviews
super后,我调整自定义活动视图的框架以匹配。这都包含在自定义UIRefreshControl
子类中。
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.
注意:我添加了 UIView 的布局边距,这并不是绝对必要的,但为我的活动指示器提供了一些喘息的空间。